Results 1 to 2 of 2
- 12-24-2011, 12:13 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Need help with ArrayList please...
Hi,
When I run this code:
I get the output:Java Code:ArrayList<Integer> possibleNumbers = new ArrayList<Integer>(); for(int i = 0;i<4;i++) { possibleNumbers.add(i); } for(int element:possibleNumbers) { System.out.println(possibleNumbers.get(element)); }
0
1
2
3
But I do not want my ArrayList to include 0. I want it to start from 1. So I modified my code:
and the output is:Java Code:ArrayList<Integer> possibleNumbers = new ArrayList<Integer>(); for(int i = 1;i<4;i++) { possibleNumbers.add(i); } for(int element:possibleNumbers) { System.out.println(possibleNumbers.get(element)); }
2
3Exception in thread "main"
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at myPackage.avdas.main(avdas.java:15)
Can anyone help me with this?
- 12-24-2011, 01:31 PM #2
Re: Need help with ArrayList please...
Change your code to put very large numbers into the arraylist:
and see what happens?Java Code:possibleNumbers.add(i*100);
The contents of the arraylist do not correspond to the indexes to the elements.
Also add this to the beginning of the second loop:
Java Code:System.out.println("el=" + element);// show the value of the index
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
copying contents of an ArrayList to another ArrayList
By ankit1801 in forum New To JavaReplies: 8Last Post: 03-27-2011, 06:07 AM -
sorting arraylist based on another arraylist
By busdude in forum New To JavaReplies: 4Last Post: 02-07-2011, 11:48 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks