| | |
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...
| Thread Tools | Search this Thread |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads transforms tree ui web windows





