XMLファイルから読み取るこのコードがあります。5つの文字列(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
...