使用数组列表创建对象并清除对象而不清除对象中的数组列表
问题描述:
如何使用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
。
因此,您创建了一些对象将它们放入某个范围的数组列表中,清除该范围内的数组列表并希望对象仍然在这里?为什么??这个问题有点像[X/Y问题](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – 2017-05-30 10:45:17