?
Solved

arraylist of arraylists

Posted on 2003-03-09
3
Medium Priority
?
1,300 Views
Last Modified: 2008-03-10
I am trying to create an arraylists of arraylists in java. To print off the data in each array list I use this method:

void printAL(ArrayList arrlist)
{
     StringBuffer strbuf = new StringBuffer();
     for (int i = 0; i < arrlist.size(); i++) {
          String bla = (String)arrlist.get(i);
          strbuf.append(bla);
     }
     System.out.println(strbuf.toString());
}

then the code for adding data to the main arraylist is something like this:

ArrayList mainarrlist = new ArrayList();
while (condition) {
        /* Code for getting a single array list called arrlisttemp */
        printAL(arrlisttemp); // this works at this point
        mainarrlist.add(arrlisttemp);
        arrlisttemp.clear();
}

// then code to get and print the array lists back:
for (int i = 0; i < mainarrlist.size(); i++) {
     System.out.print(i+": ");
        printAL((ArrayList)mainarrlist.get(i)); // this doesnt work
}

Any idea as to why it doesn't work?
0
Comment
Question by:maUru
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:yongsing
ID: 8100279
It's printing nothing because you clear the array after you add it into the main array:

mainarrlist.add(arrlisttemp);
arrlisttemp.clear(); // remove this line

So what you can do is to remove:

arrlisttemp.clear();
0
 
LVL 92

Accepted Solution

by:
objects earned 720 total points
ID: 8100372
You need to add a copy of the ArrayList each time:

ArrayList mainarrlist = new ArrayList();
while (condition) {
       /* Code for getting a single array list called arrlisttemp */
       printAL(arrlisttemp); // this works at this point
       mainarrlist.add(arrlisttemp.clone());
       arrlisttemp.;
}

Or alternatively create a new ArrayList for each loop:

ArrayList mainarrlist = new ArrayList();
while (condition) {
       ArrayList arrlisttemp = new ArrayList();
       /* Code for getting a single array list called arrlisttemp */
       printAL(arrlisttemp); // this works at this point
       mainarrlist.add(arrlisttemp);
}

0
 
LVL 92

Expert Comment

by:objects
ID: 8100378
oops the first option should be:

ArrayList mainarrlist = new ArrayList();
while (condition) {
      /* Code for getting a single array list called arrlisttemp */
      printAL(arrlisttemp); // this works at this point
      mainarrlist.add(arrlisttemp.clone());
      arrlisttemp.clear();
}

Though I'd suggest the second option.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question