每次attributeList
将值“添加”到largeAttributeList
时,如何清空?我已经尝试过.clear(),但是largeAttributeList会丢失所有值。
ArrayList<String> attributeList = new ArrayList<String>();
ArrayList<ArrayList<String>> largeAttributeList = new
ArrayList<ArrayList<String>>();
for (int i = 0; i < attribute.getLength(); i++) {
String current = attribute.item(i).getTextContent();
if(current.equals("Identifier")){
largeAttributeList.add(attributeList);
}
else{
attributeList.add(current);
}
}
最佳答案
您可以在循环中初始化数组:
....
ArrayList<String> attributeList;
for (int i = 0; i < attribute.getLength(); i++) {
String current = attribute.item(i).getTextContent();
if (current.equals("Identifier")) {
largeAttributeList.add(attributeList);
attributeList = new ArrayList<>();//<<<-------------
} else {
attributeList.add(current);
}
}
关于java - 循环清空arraylist,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44282599/