在java中的Multiple ArrayList中访问对象的属性

时间:2017-03-04 15:25:19

标签: java object arraylist vector

我只写了必要的信息,让您了解错误。 我有一个多个arraylist(规范),即

speci={{node,node},{node,node}}

节点是一个包含int和boolean的对象,即

node= int value , boolean explored

我想通过以下方式访问节点对象的int,

System.out.println(speci.get(0).get(0).value); // IDE is not accepting it, suggest a way

我的IDE(NetBeans8.1)不接受,说无法找到符号值。如何使用ArrayList访问该值? TIA :-)

1 个答案:

答案 0 :(得分:0)

node类中为字段定义getter,然后使用 -

System.out.println(speci.get(0).get(0).getValue());

根据一个假设,speci有点 -

ArrayList<ArrayList<node>> speci;

node(会建议将其重命名为Node)就像 -

public class Node {
    int value;
    ... // other attributes
    int getValue() {
        return value;
    }
    ...// other getters and setters
}

以下是Why use getters and setters?

上的有用链接
相关问题