Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

dynamic array/ arraylist of arraylists

Thread view: 
standshik - 11 Feb 2006 05:37 GMT
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
Roedy Green - 11 Feb 2006 06:34 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

N Mulangi - 11 Feb 2006 17:13 GMT
> okay i'm new to java. i need to read some stuff from a file and store
> into some data structure.
[quoted text clipped - 16 lines]
> 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));
   }
}

Signature

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

Java and Javascript Help, Tutorials, Reference:
http://www.HyperFaqs.org/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.