如何创建带有 arraylist
的对象然后当我清除arraylist
时我希望该值仍然位于对象内部。
for (int i = 0; i < attribute.getLength(); i++) {
String current = attribute.item(i).getTextContent();
if(current.equals("Identifier")){
String hyperlink = list.get(count).hyperlink;
String title = list.get(count).title;
String identifier = list.get(count).identifier;
ItemAndAttributeObject object = new ItemAndAttributeObject(hyperlink, identifier, title, attributeList);
objectList.add(object);
count++;
attributeList.clear();
}
else{
attributeList.add(current);
}
}
当我运行此代码时,所有 ItemAndAttributeObject
属性为空。如何保留对象中的值?
这就是我的类创建对象的样子
public class ItemAndAttributeObject {
String hyperlink;
String identifier;
String title;
ArrayList<String> attributes;
public ItemAndAttributeObject(String hyperlink, String identifier, String title, ArrayList<String> attributes) {
this.hyperlink = hyperlink;
this.identifier = identifier;
this.title = title;
this.attributes = attributes;
}
}
我想要创建一个带有 arraylist
的对象值,然后清除数组以进行循环中的下一次迭代。
最佳答案
不要继续使用和清除同一个ArrayList
。每次创建一个新的ArrayList
。
关于java - 用arraylist创建对象并清除,但不清除对象中的arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44259946/