Extracting a List from a List of lists

D

Damo

Hi,
I have an ArrayList of ArrayLists. I want to extract all the lists,
but I dont know how many ArrayLists will be in the ArrayList.
I know I can do it if i know how many lists are there using the
ArrayList get() method.

this is how i'm doing it

List<String> list1 = new ArrayList<String>();
list1 = res.get(0);
List<String> list1 = new ArrayList<String>();
list2 = res.get(1);
List<String> list1 = new ArrayList<String>();
list3 = res.get(2);

But if theres only two lists in the list i get a NullPointerException

Is there any way i can loop through the list and extract the lists

thanks
 
M

Michael Rauscher

Damo said:
Hi,
I have an ArrayList of ArrayLists. I want to extract all the lists,
but I dont know how many ArrayLists will be in the ArrayList.
I know I can do it if i know how many lists are there using the
ArrayList get() method.

this is how i'm doing it

List<String> list1 = new ArrayList<String>();
list1 = res.get(0);

The first line is... nonsense. You don't need to allocate an ArrayList
at this point since you get a List said:
Is there any way i can loop through the list and extract the lists

Several. I assume that res is declared something like

List<List<String>> res;


1. for ( int i = 0, n = res.size(); i < n; i++ )
List<String> l = res.get(i);

2. Iterator<List<String>> it = res.iterator();
while ( it.hasNext() )
List<String> l = it.next();

3. for ( List<String> l : res )
...

Bye
Michael
 
O

Oliver Wong

Damo said:
Hi,
I have an ArrayList of ArrayLists. I want to extract all the lists,
but I dont know how many ArrayLists will be in the ArrayList.
I know I can do it if i know how many lists are there using the
ArrayList get() method.

this is how i'm doing it

List<String> list1 = new ArrayList<String>();
list1 = res.get(0);
List<String> list1 = new ArrayList<String>();
list2 = res.get(1);
List<String> list1 = new ArrayList<String>();
list3 = res.get(2);

But if theres only two lists in the list i get a NullPointerException

Is there any way i can loop through the list and extract the lists

Check the javadocs for ArrayList. There's a method to get its size or
length, which will be the number of inner lists contained by the outer list.

- Oliver
 
D

Damo

List<List<String>> res;


1. for ( int i = 0, n = res.size(); i < n; i++ )
List<String> l = res.get(i);


2. Iterator<List<String>> it = res.iterator();
while ( it.hasNext() )
List<String> l = it.next();


3. for ( List<String> l : res )


But if I use those ways you mentioned , each time around the loop l
will contain a different list, i wont be using the lists in the loop.
so at the end of the loop i will only have one list, ie the last one.
 
L

Lew

It helps other readers if you distinguish who wrote what.

Michael said:
List<List<String>> res;

1. for ( int i = 0, n = res.size(); i < n; i++ )
List<String> l = res.get(i);

2. Iterator<List<String>> it = res.iterator();
while ( it.hasNext() )
List<String> l = it.next();

3. for ( List<String> l : res )
Damo said:
But if I use those ways you mentioned , each time around the loop l
will contain a different list, i wont be using the lists in the loop.
so at the end of the loop i will only have one list, ie the last one.

That has nothing to do with using Michael's suggestion but with what you do
with the result each time through the loop. The assumption in Michael's
suggestion is that you will

do something useful with each list

during each iteration.

You cannot expect newsgroup answers to write out every line of code. You
should impute the missing parts. If you "won't be using the lists in the loop"
then you only have yourself, not Michael, to blame.

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top