dynamic array/ arraylist of arraylists

Discussion in 'Java' started by standshik, Feb 11, 2006.

  1. standshik

    standshik Guest

    okay i'm new to java. i need to read some stuff from a file and store
    into some data structure.
    say i have the following in a file
    A B C
    D E
    F G H I

    no i need to store such a way so that i can access (i,j)th element at
    anypoint of time.


    i can do that very easily in C++ using vector of vectors of string.


    how do i do that in java? i tried using arraylist of arraylists but
    could not get it right?


    Can you somebody please explain in details how can i do that? possibly
    with a small fragmant of code?


    thanks
    kaushik
     
    standshik, Feb 11, 2006
    #1
    1. Advertising

  2. standshik

    Roedy Green Guest

    On 10 Feb 2006 21:37:15 -0800, "standshik" <>
    wrote, quoted or indirectly quoted someone who said :

    >how do i do that in java? i tried using arraylist of arraylists but
    >could not get it right?


    You probably screwed up on the initialisation. You must create a
    mother ArrayList and 3 child ArrayLists and put them in the mother
    before you can even think about filing your objects.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 11, 2006
    #2
    1. Advertising

  3. standshik

    N Mulangi Guest

    standshik wrote:
    > okay i'm new to java. i need to read some stuff from a file and store
    > into some data structure.
    > say i have the following in a file
    > A B C
    > D E
    > F G H I
    >
    > no i need to store such a way so that i can access (i,j)th element at
    > anypoint of time.
    >
    >
    > i can do that very easily in C++ using vector of vectors of string.
    >
    >
    > how do i do that in java? i tried using arraylist of arraylists but
    > could not get it right?
    >
    >
    > Can you somebody please explain in details how can i do that? possibly
    > with a small fragmant of code?
    >
    >
    > thanks
    > kaushik
    >


    This should do all you ask for:

    import java.util.ArrayList;
    import java.util.Iterator;

    public class array
    {
    static public void main(String[] args)
    {
    ArrayList parent = new ArrayList();
    {
    ArrayList child = new ArrayList();
    child.add( "A" );
    child.add( "B" );
    child.add( "C" );
    parent.add( child );
    }

    {
    ArrayList child = new ArrayList();
    child.add( "D" );
    child.add( "E" );
    parent.add( child );
    }

    {
    ArrayList child = new ArrayList();
    child.add( "F" );
    child.add( "G" );
    child.add( "H" );
    child.add( "I" );
    parent.add( child );
    }

    /*
    * Following prints "parent has 3 elements" which is what we
    * expect
    */
    System.out.println("parent has " +parent.size()+ " elements." );

    /*
    * The following loop prints:
    * A B C
    * D E
    * F G H I
    */
    for (Iterator parentIterator = parent.iterator() ;
    parentIterator.hasNext() ;
    ) {
    ArrayList child = (ArrayList)parentIterator.next();
    for (Iterator childIterator = child.iterator() ;
    childIterator.hasNext() ;
    ) {
    String str = (String)childIterator.next();
    System.out.print(str + " " );
    }
    System.out.println();
    }
    System.out.println();

    /*
    * Accessing individual elements of the array -- Need the ugly
    * typecasts (with Java 1.4)
    */
    System.out.println("Element ( 1, 1 ) is: " +
    ((ArrayList)parent.get(1)).get(1));
    System.out.println("Element ( 0, 2 ) is: " +
    ((ArrayList)parent.get(0)).get(2));
    System.out.println("Element ( 2, 2 ) is: " +
    ((ArrayList)parent.get(2)).get(2));
    System.out.println();

    /*
    * Access an element outside the range, and you get an
    * java.lang.IndexOutOfBoundsException
    */
    System.out.println("Element ( 1, 3 ) is: " +
    ((ArrayList)parent.get(1)).get(3));
    }
    }


    --
    N Mulangi [Remove "lovesspam" from email address to reply]

    Java and Javascript Help, Tutorials, Reference:
    http://www.HyperFaqs.org/
     
    N Mulangi, Feb 11, 2006
    #3
    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?UG9udGlNYXg=?=

    ArrayList of ArrayLists: How to implement IEnumerable

    =?Utf-8?B?UG9udGlNYXg=?=, Jul 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    449
    =?Utf-8?B?UG9udGlNYXg=?=
    Jul 5, 2004
  2. Replies:
    5
    Views:
    609
    Karl Seguin
    Dec 8, 2005
  3. Replies:
    2
    Views:
    2,009
  4. news.amnet.net.au
    Replies:
    6
    Views:
    81,974
    Roedy Green
    Apr 23, 2004
  5. Gary Newell

    an Array of ArrayLists in Java 5

    Gary Newell, Dec 13, 2004, in forum: Java
    Replies:
    2
    Views:
    25,064
    Gary Newell
    Dec 14, 2004
Loading...

Share This Page