0

I wonder how I create arraylists dynamically and add them to an arraylist that holds all arraylists? I also wonder how I read from a dynamically created arraylist inside the main arraylist in a for-each loop?

3
  • 1
    What are you trying to achieve? Commented May 13, 2013 at 11:44
  • An ArrayList can contain any kind of objects, including another ArrayList. There's nothing special about that. What exactly are you having trouble with understanding? Commented May 13, 2013 at 11:44
  • I'm working with an app that need multiple touch inputs and I want to store all points of each user in an arraylist and then add it to a main arraylist. From the main arraylist I can then read all users touch inputs(each arraylist) and draw the path on the screen Commented May 13, 2013 at 11:47

7 Answers 7

7
List<Bar> innerList = new ArrayList<Bar>();
// add Bar objects to this list
List<List<Bar>> mainList = new ArrayList<List<Bar>>();
mainList.add(innerList); // adding innerList to mainList
for(List<Bar> listElement:mainList){
     // here listElement will give you an innerList
         for(Bar bar: listElement){
            // gives you a handle to each Bar element stored in
            // listElement
         }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer! In your example you have "innerList", but what if I want to create other innerlists during runtime? How do I set names to them? Can I call them by there id/number in the mainList?
I'm not sure what you are asking but if you want to store something in key-value pair like name-List pair then use Map
I'm not sure either! :) This is complicated. What I meant was that each innerList has to be unique and then I guess I can't give them all the name "innerList"?
I think you are confusing reference variables to List with actual List objects !!!
I might do! Sorry I'm new to this! Can I access the innerLists content by looping throw the mainList by the index!?
|
3

Consider using Guava's Multimap with a key of Integer.

Multimap

Specifically the ArrayListMultimap

Multimap will handle dynamically creating the inner Lists for you as needed so..

To add an element to any list within the set of lists:

Multimap<Integer, Bar> myMultimap = ArrayListMultimap.create();
myMultimap.put(1, new Bar());

List<Bar> listAtIndex5 = myMultimap.get(5);

Multimap also provides the mechanism to iterate over all elements in all inner lists using values. So you can iterate over every element in the entire collection as such:

for (Bar bar: myMultimap.values()){
    // some work on bar
}

7 Comments

Why would you simulate a two-dimensional list with a MultiMap? There are no key/value pairs here...
@jlordo The "key" is the integer index of the outter list. When you do mylist[5] you are using a key of 5. The reason I suggested this was to avoid logic checking to see if a list has already been created for a particular index and then having to create and add it prior to being able to add to this inner list. All the logic of creating the inner lists is provided and therefore much less risk of a NullPointerException.
I see your point and revoked my -1. Anyway, I would prefer a simple List<List<DataType>>..
Multimap<Integer, Bar> would only have a single List<Bar> per key Integer not multiple Lists. And Multimap.get never returns null whereas if you had a List<List<Bar>> and did outterList.get() you could get a null or an IndexOutOfRange. So to add an element to the 3rd list in a Multimap you would do map.get(2).add(myBar). This would never throw an exception. This is not as easy with a List<List<Bar>> because you have to handle the case of creating the inner List.
@jlordo thanks for accepting the point and totally understand your preference especially as List<List<>> is probably more readable for the non-guava initiated as opposed to Multimap. Cheers.
|
1

Like this..

ArrayList<DATA_TYPE> demo = new ArrayList<DATA_TYPE>();
for(DATA_TYPE i:demo)//foreach
{
System.out.println(i); 

}

1 Comment

You might wanna include the generics.
1
ArrayList< ArrayList< Foo > > masterList = new ArrayList< ArrayList< Foo > >( );

ArrayList< Foo > subList = new ArrayList< Foo >();

subList.add( new Foo( ) );
subList.add( new Foo( ) );

msterList.add( subList ) );

// repeat

for ( ArrayList< Foo > subList : masterList )
{
  for ( Foo foo : subList )
  {
    System.out.println( foo );
  }
}

Comments

1

you can do that the same way you would do it with any object.

to add to an array list:

ArrayList<ArrayList> parent = new ArrayList<ArrayList>();
ArrayList<YourDataType> sub = new ArrayList<YourDataType>();
sub.add(new YourDataType());
parent.add(sub);

and when you want to read from that list:

for (ArrayList<YourDataType> list : parent){
     for(YourDataType obj : list){

        //access obj
      }

}

if you want to access a sub ArrayList at specific index:

int index = 5;
if(index < parent.size()){
    ArrayList<YourDataType> sub = parent.get(index);
}

Take a look here for a complete tutorial on Lists in Java:

3 Comments

But how do I set the names of the "sub" list during runtime?
you don't need to give them special variable names, because all of them well be appended to the parent ArrayList, and you will access them through it
OK, so I use an index to access the sub lists in the parent list? Could you add some code that show how?
0
List list= new ArrayList();<\`pre`>
List list1= new ArrayList();
list1.add("Example1");
list1.add("Example2");
list.add(list1);
System.out.println(list);

U can find two arraylist which are added inside one more <`pre`>

1 Comment

Generics were introduced with Java 1.5. Please use them.
0

You can achieve that using following lines

To declare the list dynamically

ArrayList<Object> childList = new ArrayList<Object>();
ArrayList<ArrayList<Object>> mainList=new ArrayList<ArrayList<Object>>();

To add child lists to main list

  mainList.add(childList);
  mainList.add(new ArrayList<String>());
  mainList.add(new ArrayList<Integer>());
  ........   

To Access the list inside a for loop,

for(ArrayList listobj: mainList){
   for(Object obj : listobj){
     //do something here
  }
}

If you want all your lists to be of same type you can use that type instead of Object in childlist declaration, However if you want all your child lists to be differrent it is better to use Object class directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.