Results 1 to 4 of 4
  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    112
    Rep Power
    0

    Default Array Out of Bounds, But Why?

    I am having trouble with an array out of bounds exception. I understand what the error means (that I am trying to access part of array that does not exist). But I do not see the error in my code. Could someone please point out my mistake as I have looked at it so long that it is all just blurring together. Thanks

    Java Code:
    public class bubbleSort
    {
      public static void main(String []args)
      {
        int i;
        int array [] = {12,9,4,99,120,1,3,10};
        
        System.out.print("Values after the sort: \n");
        
        for (i = 0; i < array.length; i++);
        {
          bubble_srt (array, array.length);
          System.out.println (array[i] + ", ");
        }
      }
    
      public static void bubble_srt( int a[], int n ) //a = array, n = array length
      {
         int i;
         int j;
         int t = 0;
         for (i = 0; i < n; i++)
         {
           for (j = 1; j < (n - i); j++)
           {
             if (a[j-1] < a[j])
             {
               t = a[j-1];
               a[j-1] = a[j];
               a[j] = t;
             }
           }
         }
      }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Array Out of Bounds, But Why?

    First of all, I think you should move your bubble_srt call out of the loop. You only need one sort run, not eight.

    Second, there is a semicolon right after the for loop. The System.out.println only runs once, and it uses the last value of i, which is 8 when the loop is finished.

    For counters, you would suffice by declaring them in the loop itself:
    Java Code:
            bubble_srt (array, array.length);
    
            for (int i = 0; i < array.length; i++) {
                System.out.println (array[i] + ", ");
            }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    112
    Rep Power
    0

    Default Re: Array Out of Bounds, But Why?

    Thank you so much. I can't believe I missed that. Usually I make the mistake of missing a semicolon, not adding an additional one.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Array Out of Bounds, But Why?

    When you just want to access the array elements in sequence, you should use the enhanced for loop construct:

    Java Code:
    for (int val : array) {
        System.out.println(val);
    }
    Or even better in your situation:

    Java Code:
    System.println(Arrays.toString(array));
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Array out of bounds
    By cfc1252 in forum New To Java
    Replies: 5
    Last Post: 01-11-2012, 04:21 PM
  2. Out of Bounds Array...sometimes
    By lampposteffect in forum New To Java
    Replies: 12
    Last Post: 04-21-2011, 07:10 PM
  3. 2d Array, Array out of bounds exception
    By Bmorebob in forum Advanced Java
    Replies: 2
    Last Post: 03-24-2011, 09:34 PM
  4. array going out of bounds?
    By jabo in forum New To Java
    Replies: 9
    Last Post: 04-02-2010, 10:08 AM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 01:20 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
  •