| | |
nested Arraylists
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
The following snippet throws an Exception in printin part....
can anyone explain why...
thanks in advance...
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 thrownthanks in advance...
Correct me IF i'm wrong...
Regards,
Gowtham
Regards,
Gowtham
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
If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
you have nested array lists here (as you know. )
java Syntax (Toggle Plain Text)
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>(5); /*create a new arraylists of arraylist so outer arraylist has 5 element which means size of a=5*/ for(int i = 0 ; i < a.size() ; i++){ a.set(i, new ArrayList<Integer>(10)); /*loop over 5 elements of a and put an arraylist of 10 integers in the lists.*/ } /*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.*/ System.out.println(a.get(a.size()-1).get(9));//exception thrown
![]() |
Similar Threads
- Program Help Using For Nested Loops (C++)
- nested for loop problem (Java)
- Nested For Loops (C++)
- program w/ switch AND nested if. six error messages (C++)
- query: allocating memory to nested linked lists (C)
- QBasic Nested for loop (Legacy and Other Languages)
- MySQL nested query / joined query conversion help (MySQL)
Other Threads in the Java Forum
- Previous Thread: Why??
- Next Thread: newbie...applet db doesn't work at web browser...
Views: 360 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android api apple applet application arguments array arrays automation binary block bluetooth chat class classes client code compile component database developmenthelp draw eclipse encode error event exception file fractal freeze game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number object online oracle print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing system template test textfields threads time title transfer tree tutorial-sample update windows working





