Results 1 to 6 of 6
  1. #1
    grahamb314 is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    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)

    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 + " ");
        }
       }
    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!

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    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:
    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]
           }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    grahamb314 is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote 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:
    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]
           }
    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:
    Java Code:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,250
    Rep Power
    19

    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

  5. #5
    grahamb314 is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    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?

    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 + " ");
          }    
        }
       }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,250
    Rep Power
    19

    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

Similar Threads

  1. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 08:16 AM
  2. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  3. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 01:20 AM
  4. Replies: 0
    Last Post: 11-14-2007, 03:22 PM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •