Collections 如何使用迭代器获取List的值

Collections 如何使用迭代器获取List的值,collections,Collections,如何获取comp1和comp2的值。我尝试过使用迭代器,但它给了我一个异常 更新:如果我必须在另一个类中获取值,我如何才能做到这一点 public class Complex { public List<String>comp1; public List<String>comp2; public List<String> getComp2() { return comp2; } public void setComp2(List<St

如何获取comp1和comp2的值。我尝试过使用迭代器,但它给了我一个异常

更新:如果我必须在另一个类中获取值,我如何才能做到这一点

public class Complex {


 public List<String>comp1;
 public List<String>comp2;

public List<String> getComp2() {
    return comp2;
}

public void setComp2(List<String> comp2) {
    this.comp2 = comp2;
}

public List<String> getComp1() {
    return comp1;
}

public void setComp1(List<String> comp1) {
    this.comp1 = comp1;
}
}


comp2也是一样。

代码对我来说很好。在调用setter之后,您可以调用getter。您在这里发布的代码示例没有多大帮助。也许你指的是字符串值:comp1{System.out.printlnvalue;},它在内部使用迭代器。一般来说,看看这里:如果我必须在另一个类中获取值,那么我应该如何调用?get方法返回列表,然后你可以在另一个类中迭代它。
Iterator<String> it = comp1.getIterator();
while(it.hasNext()) {
   //do something with it
   it.next();
}