ArrayList.size()返回字段的大小,而不是List的大小,为什么?

时间:2018-07-23 10:41:28

标签: java object arraylist size

我有一个ArrayList ,其中Entity是具有5个字段的对象。现在,如果我要遍历LIST,尽管List肯定比1000长,但ArrayList.size()返回5,我已经通过计算将实体添加到ArrayList的次数进行了检查。 有人可以帮我吗?

实体只是一个具有5个具有getter和setter方法的字段的对象:

public class Entity{
private String FirstField;
...
private String FithField;
}

比起将实体添加到ArrayList:

final ArrayList<Entity> EntityArray = new ArrayList<Entity>();
for(certain range){
Entity E = new Entity;
E = SomeFunctionWithEntityOutput();
EntityArray.add(E);
}

然后size()方法返回5:

EntityArray.size()

那好吗?

问候!

1 个答案:

答案 0 :(得分:-7)

请注意,您不需要在arraylist的大小上进行迭代。您只需完成

for (Entity e : myList){
    //Do stuff with e
}

如果您确实需要索引,因此需要调用.size(),请发布一些示例代码,以便其他人可以查看出了什么问题。

相关问题