我有这个从 XML 文件中读取的代码。它获取五个字符串(groupId、groupType、filePath、author 和 lineNo),并首先将它们保存在一个字符串数组中。然后,字符串数组被保存在一个 ArrayList 中。最后,最后一个“for”显示了 ArrayList 的内容。
当我想显示内容时,我只得到最后添加的字符串数组的问题。以下是代码和输出。谁能弄清楚是什么问题?
ArrayList<String[]> developerTypes = new ArrayList<String[]>();
String[] developerInfo = {null, null, null, null, null};
String[] developerInfoR = {null, null, null, null, null};
String groupId;
String groupType;
String filePath;
String author;
String lineNo;
SAXBuilder builder = new SAXBuilder();
Document doc = (Document) builder.build("A.xml");
Element clones = doc.getRootElement();
// Loop of clones' children (clone_group)
List<Element> parentElements = clones.getChildren();
for(Element parentElement:parentElements){
// Loop of clone_group's children (clone_fragment)
List<Element> elements = parentElement.getChildren();
for(Element element:elements){
// Loop of clone_fragment's children (blameInfo)
List<Element> childelements = element.getChildren();
for(Element childElement:childelements){
groupId = parentElement.getAttributeValue("groupid");
groupType = parentElement.getAttributeValue("type");
filePath = element.getAttributeValue("file");
author = childElement.getAttributeValue("author");
lineNo = childElement.getAttributeValue("lineNo");
//System.out.print(groupId + " - ");
//System.out.print(groupType + " - ");
//System.out.print(file + " - ");
//System.out.println(author);
developerInfo[0] = groupId;
developerInfo[1] = groupType;
developerInfo[2] = filePath.substring(1, filePath.lastIndexOf("."));;
developerInfo[3] = author;
developerInfo[4] = lineNo;
developerTypes.add(developerInfo);
}// for (blameInfo)
}// for (clone_fragment)
}// for (clone_group)
// Display the content of the Arraylist
for(int i = 0; i< developerTypes.size(); ++i){
developerInfoR = developerTypes.get(i);
for(int j = 0; j< developerInfoR.length; ++j){
System.out.print(developerInfoR[j] + " ");
}
System.out.print("\n");
}
输出:
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
309 Type-3 builtin/update-index.c Jonathan Nieder 704
...