943,916 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1018
  • Java RSS
Jun 22nd, 2009
0

nested Arraylists

Expand Post »
The following snippet throws an Exception in printin part....
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>(5);
        for(int i = 0 ; i < a.size() ; i++){
            a.set(i, new ArrayList<Integer>(10));
        }
        System.out.println(a.get(a.size()-1).get(9));//exception thrown
can anyone explain why...

thanks in advance...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gowth08 is offline Offline
9 posts
since Jun 2009
Jun 22nd, 2009
0

Re: nested Arraylists

there's nothing in the value/its not initialized. I don't think you have to initialize the arraylists inside the first one, they're already there. You just have to initialize the numbers inside. ;D
Last edited by JugglerDrummer; Jun 22nd, 2009 at 5:25 pm.
Reputation Points: 14
Solved Threads: 22
Junior Poster
JugglerDrummer is offline Offline
138 posts
since Apr 2009
Jun 22nd, 2009
0

Re: nested Arraylists

you have nested array lists here (as you know. )

java Syntax (Toggle Plain Text)
  1. ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>(5);
  2. /*create a new arraylists of arraylist so outer arraylist has 5 element which means size of a=5*/
  3. for(int i = 0 ; i < a.size() ; i++){
  4. a.set(i, new ArrayList<Integer>(10));
  5. /*loop over 5 elements of a and put an arraylist of 10 integers in the lists.*/
  6. }
  7. /*at this point you got object named a (arraylist of arraylists of Integer objects) but you haven't put any integer in inner arraylist. so a.get(4).get(9) is not assigned yet.*/
  8. System.out.println(a.get(a.size()-1).get(9));//exception thrown
Reputation Points: 31
Solved Threads: 5
Light Poster
yilmazhuseyin is offline Offline
48 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Why??
Next Thread in Java Forum Timeline: newbie...applet db doesn't work at web browser...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC