Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 06:28 PM
Member
 
Join Date: Nov 2008
Posts: 8
Rep Power: 0
grahamb314 is on a distinguished road
Default 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)

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 + " ");
    }
   }
distances array print out (not an arraylist)
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!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2008, 07:43 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Too many increments?
In your for loop, you are incrementing variable "i" twice... once in the for statement and another in the for body:
Code:
for (i = 0; i<tempCities.size(); i++  ) {
       if (( Integer ) cities.get(i) == 0) {   //ERROR IS ON THIS LINE
           i++;//<-Is this supposed to be here? ALready have i++ above in for statement
       }
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2008, 07:49 PM
Member
 
Join Date: Nov 2008
Posts: 8
Rep Power: 0
grahamb314 is on a distinguished road
Default
Originally Posted by CJSLMAN View Post
In your for loop, you are incrementing variable "i" twice... once in the for statement and another in the for body:
Code:
for (i = 0; i<tempCities.size(); i++  ) {
       if (( Integer ) cities.get(i) == 0) {   //ERROR IS ON THIS LINE
           i++;//<-Is this supposed to be here? ALready have i++ above in for statement
       }
Luck,
CJSL
Yea, the idea is if the element i am currently looking at = 0 then try the next one

I took it out and get the same error:
Code:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2008, 07:58 PM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 716
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
> 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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-22-2008, 08:07 PM
Member
 
Join Date: Nov 2008
Posts: 8
Rep Power: 0
grahamb314 is on a distinguished road
Default
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?

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 + " ");
      }    
    }
   }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-22-2008, 08:21 PM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 716
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] out of bounds exception help soxfan714 New To Java 21 11-11-2008 09:16 AM
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 12:43 PM
why is my array out of bounds? Phobos0001 New To Java 3 03-24-2008 02:20 AM
Iterating through ArrayList - using newly introduce for loop in Java 5.0 Java Tip Java Tips 0 11-14-2007 04:22 PM
ArrayList: Exception in thread "main" java.lang.NullPointerException susan New To Java 1 07-16-2007 07:32 AM


All times are GMT +2. The time now is 12:01 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org