0

我有这个从 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 
...
4

3 回答 3

3

当我想显示内容时,我只得到最后添加的字符串数组的问题。

不,你发现你有很多同一个字符串数组的引用......因为那是你添加的。你只有一个字符串数组对象developerInfo只是对该数组的引用。当您调用时developerTypes.add(developerInfo),会将引用复制到 中ArrayList,因此您多次获得相同的引用。

您应该将声明和实例化拉developerInfo入循环:

String[] developerInfo = {
    groupId,
    groupType,
    filePath.substring(1, filePath.lastIndexOf(".")),
    author,
    lineNo
};
developerTypes.add(developerInfo);

同样,如果您在使用它之前不声明,您的代码会更干净developerInfoR

for(int i = 0; i< developerTypes.size(); ++i){
    String[] developerInfoR = developerTypes.get(i);
    for(int j = 0; j< developerInfoR.length; ++j){
        System.out.print(developerInfoR[j] + " ");
    }
    System.out.print("\n");
}

或者更好的是,使用增强的 for 循环:

for (String[] developerInfoR : developerTypes) {
    for (String info : developerInfoR) {
        System.out.print(info + " ");
    }
    System.out.print("\n");
}

一般来说,您应该尽可能晚地声明具有最小范围的局部变量,理想情况下在声明时分配一个值。在方法顶部声明所有变量确实会损害可读性。

于 2012-06-27T20:09:50.163 回答
2

您只有一个developerInfo在循环中不断添加的数组实例。您需要在每个循环步骤中创建一个新数组。

您似乎对您正在使用的构造的语义有一些误解。我从您预先初始化的方式推断出这一点developerInfoR,但从不使用该值。数组变量只包含对数组的引用,因此每次分配给它时,之前引用的数组都会被遗忘并丢弃。所以,不要预先初始化developerInfo。事实上,你甚至不需要在你使用它的地方声明它。

还有一种更简单的方法来打印数组的内容:只需调用System.out.println(Arrays.toString(developerInfoR)). 这样,您甚至不需要打印代码中的内部循环。

您不需要任何变量 groupId、groupType、filePath、author、lineNo。只写例子developerInfo[0] = parentElement.getAttributeValue("groupid");和类似的。这使得代码更明显。但是使用完整对象而不是 String[] 可能更有意义。有很多字段,每个字段的含义都在整数索引后面。

总而言之,采用这些建议加上一些额外的建议,您的代码可以重写为:

final String path = filePath.substring(1, filePath.lastIndexOf("."));
final Document doc = (Document) new SAXBuilder().build("A.xml");
final List<DeveloperInfo> developers = new ArrayList<String[]>();
for (Element parentElement : doc.getRootElement().getChildren())
  for (Element element : parentElement.getChildren())
    for (Element childElement : element.getChildren())
      developers.add(new DeveloperInfo(
          parentElement.getAttributeValue("groupid"),
          parentElement.getAttributeValue("type"),
          element.getAttributeValue("file"),
          path,
          childElement.getAttributeValue("author"),
          childElement.getAttributeValue("lineNo"),
      ));
for (DeveloperInfo d : developerTypes) System.out.println(d);

DeveloperInfo 类:

class DeveloperInfo {
  public final String groupId, groupType, filePath, author, lineNo;
  public DeveloperInfo(
      String groupId, String groupType, String filePath, 
      String author, String lineNo)
  {
    this.groupId = groupId; this.groupType = groupType; this.filePath = filePath;
    this.author = author; this.lineNo = lineNo;
  }
  public String toString() {
    return "DeveloperInfo [groupId=" + groupId + ", groupType=" + groupType + 
      ", filePath=" + filePath + ", author=" + author + ", lineNo=" + lineNo + "]";
  }
于 2012-06-27T20:09:40.280 回答
0

每次将其添加到ArrayList.

而不是声明这个String[] developerInfo = {null, null, null, null, null};

一开始。每次循环时创建一个新数组。

String[] developerInfo = new String[5];

于 2012-06-27T20:10:22.067 回答