ArrayList of ArrayLists - how to get the values out?

Discussion in 'Java' started by news.amnet.net.au, Apr 23, 2004.

  1. Hi

    I found that I need to store a collection of objects as ArrayLists inside
    another ArrayList, because if I store my (objects in the) ArrayLists in a
    Map type Collection, iterating over the map does not give me first key in ->
    first key out.

    It was suggested to me I use LinkedHashMap, but that only exists in Java
    platform 1.4 and I am working with 1.3.1 as this is what is available within
    my Oracle environment.

    Hopefully an ArrayList of ArrayLists will give me FIFO.

    Excuse my ignorance on this one as I have never used an ArrayList of
    ArrayLists - my question is: how do I get the values out?

    My "outer" ArrayList is called indexed_relationships and the inner ones are
    all called "p", (both have been declared and initialised earlier on) I
    tried:

    I have tried several things, but none work. For example, I tried ( I know
    this is wrong, but it is what I could think of):

    for(int i = 0; i < indexed_relationships.size(); i++ ) {
    for (int j = 0; j < p.size(); j++) {
    System.out.println(indexed_relationships.get(i).get(j).toString() +
    "<br/>");
    }
    }

    Any help will be greatly appreciated.

    Thanks very much

    Hugo
     
    news.amnet.net.au, Apr 23, 2004
    #1
    1. Advertising

  2. news.amnet.net.au

    Roedy Green Guest

    On Fri, 23 Apr 2004 10:32:18 +0800, "news.amnet.net.au"
    <> wrote or quoted :

    >Excuse my ignorance on this one as I have never used an ArrayList of
    >ArrayLists - my question is: how do I get the values out?


    You will get your head bitten off if you ask questions like that
    BEFORE you at least look at the JavaDoc and google for a tutorial.

    Further these are newbie questions, which belong in
    comp.lang.java.help.

    ArrayList is a quite straightforward class with few surprises other
    than you can't add elements out past the end, just at the end.

    see http://mindprod.com/jgloss/gettingstarted.html


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 23, 2004
    #2
    1. Advertising

  3. news.amnet.net.au

    Bjorn Abelli Guest

    "news.amnet.net.au" wrote...

    > for(int i = 0; i < indexed_relationships.size(); i++ )
    > {
    > for (int j = 0; j < p.size(); j++)
    > {
    > System.out.println(
    > indexed_relationships.get(i).get(j).toString()
    > + "<br/>");
    > }
    > }


    ArrayLists stores Objects.

    In version 1.5 you can use the generics to make an ArrayList work with only
    instances of a specific subclass to Object, but until then you need to cast
    the objects to the actual type when you're getting them out of an ArrayList.

    ((ArrayList)
    indexed_relationships.get(i)).get(j).toString()

    However, there's another jump in your thoughts here, as there's no "p"
    available. The ArrayList instances doesn't have any "names" when they are
    inside another ArrayList. Hence you cannot know the size of it "beforehand",
    unless you have the same size on all contained ArrayLists, but then you
    wouldn't even have to *check* the size.

    for(int i = 0; i < indexed_relationships.size(); i++ )
    {
    ArrayList p =
    (ArrayList)
    indexed_relationships.get(i);

    for (int j = 0; j < p.size(); j++)
    {
    System.out.println(
    p.get(j).toString() + "<br/>");
    }
    }

    > Any help will be greatly appreciated.


    You will get much more help if you post to the correct newsgroup. For
    questions concerning newbie questions such as this you will be better off in
    comp.lang.java.help.

    // Bjorn A
     
    Bjorn Abelli, Apr 23, 2004
    #3
  4. news.amnet.net.au wrote:

    > Hopefully an ArrayList of ArrayLists will give me FIFO.


    Any List implementation preserves the order of elements modulo the
    specific effects of List modifications -- that's part of what makes it a
    List. If you add elements only at the end and process only from
    beginning to end then you get FIFO behavior.

    > Excuse my ignorance on this one as I have never used an ArrayList of
    > ArrayLists - my question is: how do I get the values out?


    You get the inner ArrayLists out of the outer ArrayList one at a time,
    then for each one you get the values out, one at a time. This is not
    much different from how you would do it if the outer collection were a
    Map (or Set) rather than a List.

    > My "outer" ArrayList is called indexed_relationships and the inner ones are
    > all called "p", (both have been declared and initialised earlier on) I


    No, it isn't and they aren't. Collections are one of the places in the
    Java platform API where it is essential to understand the difference
    between objects, references to objects, and reference-type variables.
    Of the three, only the variables names. Object references can be stored
    in Collection objects, in arrays, and in variables, and the same
    reference (a value) can be stored in multiple places. The fact that a
    reference to some object was once stored in a variable of some
    particular name in some particular scope does not confer that name upon
    the reference value or the object to which it refers.

    > tried:
    >
    > I have tried several things, but none work. For example, I tried ( I know
    > this is wrong, but it is what I could think of):
    >
    > for(int i = 0; i < indexed_relationships.size(); i++ ) {
    > for (int j = 0; j < p.size(); j++) {

    -----------------------^^^^^^^^
    This is wrong, because nothing associates variable "p" with the
    particular ArrayList you want to iterate over. You need to retrieve the
    desired inner List from the outer List before starting the iteration.

    > System.out.println(indexed_relationships.get(i).get(j).toString() +

    -----------------------------------------------------^^^^^^
    This is wrong because the return value of List.get() is Object; you have
    to cast to ArrayList (or List) before invoking get(int) on the result.
    Moreover, it is dreadfully inefficent to retrieve and cast the inner
    List at each iteration of the inner loop, when presumably the outer List
    hasn't changed.

    > "<br/>");
    > }
    > }


    Also, although you can do an indexed iteration as above, I generally
    recommend using an Iterator instead. At first it may seem less natural
    if you're coming from, for instance, C, but it has several advantages
    (that I won't go into at the moment) and few disadvantages.


    John Bollinger
     
    John C. Bollinger, Apr 23, 2004
    #4
  5. news.amnet.net.au

    Jason Bell Guest

    Jeez put the guy of his misery :)

    Iterator iterator = myMainArray.iterator();
    while(iterator.hasNext()){
    ArrayList tempArray = (ArrayList)iterator.next();
    Iterator newiterator = tempArray.iterator();
    while(newiterator.hasNext()){
    // do what you need to do.
    }
    }

    Is that the sort of thing you are after?

    All very well thinking about generics in JDK1.5 but it ain't production so I
    wouldn't touch it until it is so.

    Regards
    Jase Bell
     
    Jason Bell, Apr 23, 2004
    #5
  6. news.amnet.net.au

    Guest

    Peace be unto you.
    <code>import java.util.Collections;
    import java.util.ArrayList;
    import java.util.Iterator;

    public class Fifo
    {
    public static void main(String [] args)
    {
    ArrayList fruits = new ArrayList();

    ArrayList mango = new ArrayList();
    mango.add("Mango");
    mango.add("Delicious");
    mango.add("Yellow");
    fruits.add(mango);
    ArrayList apple = new ArrayList();
    apple.add("Apple");
    apple.add("Yummy");
    apple.add("Red");
    fruits.add(apple);
    ArrayList tomato = new ArrayList();
    tomato.add("Tomato");
    tomato.add("Taste like vinegar");
    tomato.add("Red");
    fruits.add(tomato);

    Collections.reverse(fruits);
    int count = 1;
    for(Iterator c = fruits.iterator(); c.hasNext();)
    {
    for(Iterator d = ((ArrayList)c.next()).iterator();
    d.hasNext();)
    {
    System.out.println((String)d.next());
    }
    System.out.println("End details of fruit #" + count);
    count++;
    }
    }
    }
    </code>
    Outputs.
    Tomato
    Taste like vinegar
    Red
    End details of fruit #1
    Apple
    Yummy
    Red
    End details of fruit #2
    Mango
    Delicious
    Yellow
    End details of fruit #3

    I am not sure about the requirements.
    Have a good day.
     
    , Apr 23, 2004
    #6
  7. news.amnet.net.au

    Roedy Green Guest

    On Fri, 23 Apr 2004 10:38:29 -0500, "John C. Bollinger"
    <> wrote or quoted :

    >
    >Any List implementation preserves the order of elements modulo the
    >specific effects of List modifications -- that's part of what makes it a
    >List. If you add elements only at the end and process only from
    >beginning to end then you get FIFO behavior.


    If you want simple FIFO queue, you will have to write it yourself.
    Oddly simple LIFO and FIFO queues were not included in the
    Collections. They are pretty easy to write. You could do a variant of
    Sun's or my LinkedList stripping out the List features.

    In the meantime you can use a LinkedList for a FIFO or LIFO queue.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 23, 2004
    #7
    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. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,741
    Natty Gur
    Aug 19, 2003
  2. =?Utf-8?B?UG9udGlNYXg=?=

    ArrayList of ArrayLists: How to implement IEnumerable

    =?Utf-8?B?UG9udGlNYXg=?=, Jul 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    429
    =?Utf-8?B?UG9udGlNYXg=?=
    Jul 5, 2004
  3. Replies:
    2
    Views:
    1,990
  4. Kaidi
    Replies:
    4
    Views:
    2,382
    Kaidi
    Jan 3, 2004
  5. standshik
    Replies:
    2
    Views:
    14,768
    N Mulangi
    Feb 11, 2006
Loading...

Share This Page