nested Arraylists

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 7
Reputation: gowth08 is an unknown quantity at this point 
Solved Threads: 0
gowth08 gowth08 is offline Offline
Newbie Poster

nested Arraylists

 
0
  #1
Jun 22nd, 2009
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...
Correct me IF i'm wrong...
Regards,
Gowtham
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 119
Reputation: JugglerDrummer is an unknown quantity at this point 
Solved Threads: 15
JugglerDrummer's Avatar
JugglerDrummer JugglerDrummer is offline Offline
Junior Poster

Re: nested Arraylists

 
0
  #2
Jun 22nd, 2009
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.
92% of all statistics are made up on the spot.

If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: nested Arraylists

 
0
  #3
Jun 22nd, 2009
you have nested array lists here (as you know. )

  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC