Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Extracting a List from a List of lists

Reply
Thread Tools

Extracting a List from a List of lists

 
 
Damo
Guest
Posts: n/a
 
      02-08-2007
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

 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      02-08-2007
Damo schrieb:
> 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<String>-object from res.get(0);

>
> 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
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      02-08-2007

"Damo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Damo
Guest
Posts: n/a
 
      02-08-2007
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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-09-2007
It helps other readers if you distinguish who wrote what.

Michael Rauscher wrote:
> 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 wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting elements over multiple lists? JoeM Python 15 11-15-2011 10:54 PM
common elements between list of lists and lists antar2 Python 2 07-17-2008 09:19 AM
How to have a list of lists (or array of lists) bahoo Python 3 04-03-2007 07:37 PM
list of lists of lists .... yomgui Python 6 07-31-2006 07:28 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM



Advertisments