Results 1 to 6 of 6
- 11-22-2008, 06:28 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
Java ArrayList out of bounds exception
Hi there,
I have some code that does various simple things to some ArrayLists that I have created.
However, I get an out of bounds exception on the line i hightlight below
I have spent what must be an hour trying to solve what must be a simple problem!
For reference: distances is a 2Darray of type string with size of intSize(Currently 12) (A print of for it is below)
Java Code:/** * * @author grahambthreeonefouratgmaildotcom * @version 1.0 * Traveling Salesman Java Project * Start: 03/Nov/08 * */ //ERROR: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11 ArrayList cities = new ArrayList<Integer>(); System.out.println(); int d=1; //Populate the arrayList with all cities) eg 1-12 where intSize=12 for(int i=0; i<intSize; i++) { cities.add(d); d++; //print Arraylist to test //System.out.println(cities.get(d-2)+ " "); } System.out.println(); System.out.println(); while (cities.size() != 0) { //while not null (ATM it will always be null till finished) int x=0; Integer currentCity = ( Integer ) cities.get(x); cities.remove(x); int i=0; ArrayList tempCities = new ArrayList<Integer>(); //get distances from this city to the rest into the Temp array list for (i=0; i<distances.length;i++) { int u = distances[currentCity-1][i]; //int u = distances[5-1][i]; tempCities.add(i,u); //print for testing //System.out.println(tempCities.get(i)+ " "); } System.out.println(); //find smallest city in the temp array i = 0; int smallest = 999999999; for (i = 0; i<tempCities.size(); i++ ) { if (( Integer ) cities.get(i) == 0) { //ERROR IS ON THIS LINE i++; } Integer curr = ( Integer ) cities.get(i); if ( curr < smallest) { smallest = curr; System.out.println(smallest + " "); } }
0 53 6 8 20 40 15 7 30 29 2 3
53 0 30 35 50 5 6 10 12 20 30 32
6 30 0 4 10 15 15 9 9 9 8 3
8 35 4 0 7 21 14 13 12 11 17 8
20 50 10 7 0 30 32 34 20 6 40 25
40 5 15 21 30 0 9 14 7 13 31 32
15 6 15 14 32 9 0 4 21 25 15 16
7 10 9 13 34 14 4 0 31 35 4 10
30 12 9 12 20 7 21 31 0 5 50 40
29 20 9 11 6 13 25 35 5 0 27 21
2 30 8 17 40 31 15 4 50 27 0 5
3 32 3 8 25 32 16 10 40 21 5 0
Thanks!
- 11-22-2008, 07:43 PM #2
Too many increments?
In your for loop, you are incrementing variable "i" twice... once in the for statement and another in the for body:
Java Code:for (i = 0; i<tempCities.size(); i++ ) { if (( Integer ) cities.get(i) == 0) { //ERROR IS ON THIS LINE i++;//[COLOR="Red"]<-Is this supposed to be here? ALready have i++ above in for statement[/COLOR] }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-22-2008, 07:49 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
- 11-22-2008, 07:58 PM #4
> the idea is if the element i am currently looking at = 0 then try the next one
And what happens if the last element == 0? and there isn't a next one to try?
db
- 11-22-2008, 08:07 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
ah, okay that makes sence, but like i said, i have removed this from my code and I still get the same error .
Its definatly something to do with the way i have the for and if loops. I have moved them around etc, but no solution.
Any more thoughts?
Java Code:i = 0; int smallest = 999999999; for (i = 0; i<tempCities.size(); i++ ) { if (( Integer ) cities.get(i) == 0) { System.out.println("There is a 0"); //i++; } else{ Integer curr = ( Integer ) cities.get(i); if ( curr < smallest) { smallest = curr; System.out.println(smallest + " "); } } }
- 11-22-2008, 08:21 PM #6
Impossible to say with the snippet you posted. For example, we can't see where you initialize distances. At a quick check, it appears that tempCities.size() == distances.length. cities on the other hand is reducing in size() each time you remove() a city.
To get better help sooner, post a SSCCE : Java Glossary that clearly demonstrates your problem.
db
Similar Threads
-
[SOLVED] out of bounds exception help
By soxfan714 in forum New To JavaReplies: 21Last Post: 11-11-2008, 09:16 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 12:43 PM -
why is my array out of bounds?
By Phobos0001 in forum New To JavaReplies: 3Last Post: 03-24-2008, 02:20 AM -
Iterating through ArrayList - using newly introduce for loop in Java 5.0
By Java Tip in forum Java TipReplies: 0Last Post: 11-14-2007, 04:22 PM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 07:32 AM
Bookmarks