Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:49:10+00:00 2026-06-06T11:49:10+00:00

I have this code that reads from XML file. It gets five strings (groupId,

  • 0

I have this code that reads from XML file. It gets five strings (groupId, groupType, filePath, author, and lineNo), and it first saves them in a String Array. Then, the String Array is saved in an ArrayList. Finally, the last “for” displays the content of the ArrayList.

The problem that when I want to display the content, I get just the last added string array. The following is the code and the output. Can anyone figure out what is the problem?

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");

}

The Output:

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 
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:49:12+00:00Added an answer on June 6, 2026 at 11:49 am

    The problem that when I want to display the content, I get just the last added string array.

    No, you find that you’ve got many references to the same string array… because that’s what you’ve added. You’ve only got one string array object; developerInfo is just a reference to that array. When you call developerTypes.add(developerInfo) that’s copying the reference into the ArrayList, so you’ve got the same reference lots of times.

    You should pull the declaration and instantiation of developerInfo into the loop:

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

    Likewise your code would be cleaner if you didn’t declare developerInfoR until you used it:

    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");
    }
    

    Or even better, use the enhanced for loop:

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

    In general, you should declare local variables with the smallest scope you can get away with, as late as possible, ideally assigning a value at the point of declaration. Declaring all the variables at the top of the method really hurts readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that works fine when I call it from within the
I have a service that reads an xml document from a directory(works OK), saves
I have a program that reads arbitrary data from a file system and outputs
I have parametrized junit test that reads from several XML input files. In the
I have this method to get xml file from google reader with the contens
I have a timer that loops through following code every second. this code reads
I have written a program that reads in a File object (really an XML
I have an XML file that I'm trying to read from here , and
I have this code that draws the boxes on a Qbert board, how would
I have this code that I want to a) run when the check box

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.