Extracting a List from a List of lists

Discussion in 'Java' started by Damo, Feb 8, 2007.

  1. Damo

    Damo Guest

    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
     
    Damo, Feb 8, 2007
    #1
    1. Advertising

  2. 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
     
    Michael Rauscher, Feb 8, 2007
    #2
    1. Advertising

  3. Damo

    Oliver Wong Guest

    "Damo" <> wrote in message
    news:...
    > 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
     
    Oliver Wong, Feb 8, 2007
    #3
  4. Damo

    Damo Guest

    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.
     
    Damo, Feb 8, 2007
    #4
  5. Damo

    Lew Guest

    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
     
    Lew, Feb 9, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. yomgui

    list of lists of lists ....

    yomgui, Jul 28, 2006, in forum: Python
    Replies:
    6
    Views:
    315
    yomgui
    Jul 31, 2006
  3. bahoo
    Replies:
    3
    Views:
    306
    Bruno Desthuilliers
    Apr 3, 2007
  4. antar2
    Replies:
    2
    Views:
    394
    Bighead
    Jul 17, 2008
  5. JoeM
    Replies:
    15
    Views:
    323
    Chris Angelico
    Nov 15, 2011
Loading...

Share This Page