找不到符号charAt(int)?

时间:2017-04-05 09:40:49

标签: java arrays maps charat

有人可以告诉我这里我做错了什么。

我试图访问地图,然后输入以字母开头的键" N"在数组中。但是我找到了一个无法找到引用charAt(int)的符号错误? Intellij建议我为chartAt创建一个抽象类?

import java.util.Map;
public class RoadNetwork {

    String[] nodeList;

    public void storeNodes(Map<String, Element> result) {

        int counter =0;
        nodeList = new String[result.size()];

        //Cycle through Map to find elements which are Nodes
        for (int i = 0; i < result.size(); i++) {
            //if Node, then add it to array
            if (result.get(i).charAt(0) == "N") {
                nodeList[i] = String.valueOf(result.get(i));
                counter++;
            }
        }
        System.out.println("Nodes Array Length" + counter);
    }
}

2 个答案:

答案 0 :(得分:1)

你的Map有一个键作为字符串,你在行中传递int if(result.get(i).charAt(0)==&#34; N&#34;){ 所以不要传递result.get(int)传递result.get(String)

要检查从N开始的键,请执行以下操作:

int counter = 0;     nodeList = new String [result.size()];

//Cycle through Map to find elements which are Nodes
int i = 0;
    //if Node, then add it to array
  for(String key : result.keySet())
  {
  if (key.charAt(0) == 'N') {
        nodeList[i] = key;
        counter++;
    }
  }

System.out.println("Nodes Array Length" + counter);

答案 1 :(得分:0)

问题似乎与

有关
if (result.get(i).charAt(0) == "N") {

您可能希望检索密钥,但get()方法返回的值为Element类型,其中没有方法charAt()

您可以尝试以下方式:

for (String key:result.keySet()) {
        //if Node, then add it to array
        if (key.charAt(0) == 'N') { //'N' and not "N"
            nodeList[i] = String.valueOf(result.get(key));
            counter++;
        }
    }
相关问题