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?
7 Answers
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
}
}
6 Comments
Consider using Guava's Multimap with a key of Integer.
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
MultiMap? There are no key/value pairs here...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.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.List<List<>> is probably more readable for the non-guava initiated as opposed to Multimap. Cheers.Like this..
ArrayList<DATA_TYPE> demo = new ArrayList<DATA_TYPE>();
for(DATA_TYPE i:demo)//foreach
{
System.out.println(i);
}
1 Comment
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
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
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
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.
ArrayListcan contain any kind of objects, including anotherArrayList. There's nothing special about that. What exactly are you having trouble with understanding?