我想使我自己的类型ArrayList存储一些值。但是,我收到一个错误“x无法解析或不是字段”,例如源代码。ArrayList错误
这里是我的代码片段:
public class myClass {
public static void main(String args[]){
addEdge("a","b", 10);
}
private static void addEdge(String source, String destination, int cost) {
List<Edge> add = new ArrayList<Edge>();
add.source = source; //error: source cannot be resolved or is not a field
add.destination = destination; //error: destination cannot be resolved or is not a field
add.cost = cost; //error: cost cannot be resolved or is not a field
}
}
class Edge{
String source;
String destination;
int cost;
}
正如你可以看到我在addEdge方法出现错误。我
'add'是一个'List'。一个列表没有“来源,目的地,成本等等”字段,您需要首先使用List.get(index)来访问列表中的项*,以便能够编辑这些字段。 – GiantTree
@Sabir,答案是正确的,无论如何,我不会使用“添加”作为变量名称,因为它已经是List类的一种方法,所以它可能会被混淆。 – Drumnbass