Results 1 to 4 of 4
  1. #1
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException - sometimes happens, sometimes don't

    Hey, I'm new to Java and while doing some training execise, I got the following error (While writing a program that would sort an array of Integers, beginning with the largest value)):
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at sortarray.Main.swapElements(Main.java:91)
    at sortarray.Main.sortArray(Main.java:34)
    at sortarray.Main.main(Main.java:19)
    Java Result: 1
    Now I know what it means - that the index is either < 0 or >= Array.length(). However, the problem is that the code sometimes executer correctly, and then again it doesn't, and I haven't been able to figure out to fix it.
    Java Code:
    public static void main(String[] args) {
    
            sortArray(0, 10, 10);
        }
    Java Code:
        public static void sortArray(int low, int high, int amount) {
    
            int[] numbers = randomIntArray(low, high, amount);
    
            printArray(numbers);
    
            int i = 0;
    
            while (i < amount) {
    
                int indexHigh = indexOfMaxInRange(numbers, low, i);
    
                swapElements(numbers, indexHigh, i);
    
                i++;
            }
    
            printArray(numbers);
        }
    Java Code:
        public static int[] randomIntArray(int low, int high, int amount) {
    
            int[] random = new int[amount];
    
            int i = 0;
            while(i < amount) {
    
                random[i] = randomInt(low, high);
                i++;
            }
    
            return random;
    
        }
    Java Code:
        public static int randomInt(int low, int high) {
    
            return (int) (Math.round (Math.random() * (high-low) ) +low);
        }
    Java Code:
        //Gets the index od the largest integer in a ceratin range (Beginning at low to end)
        public static int indexOfMaxInRange(int[] numbers, int low, int beginIndex) {
    
            int i = beginIndex;
            int value = low;
            int index = -1;
    
            while (i<numbers.length) {
    
                if (numbers[i] > value) {
    
                    value = numbers[i];
                    index = i;
    
                }
    
                i++;
    
            }
    
            return index;
    
        }
    Java Code:
        //Swaps the values at two indices
        public static void swapElements(int[] x, int index1, int index2) {
    
            // x[index1] gets overwritten, so it gets stored first
            int tempValue = x[index1];
    
            x[index1] = x[index2];
            x[index2] = tempValue;
    
        }


    Java Code:
        public static void printArray(int[] x) {
    
            int i = 0;
    
            while (i < x.length) {
    
                System.out.println(x[i]);
    
                i++;
            }
    
            System.out.println("---------------\n---------------\n---------------");
        }
    Thanks for your help!
    Last edited by Boreeas; 04-09-2011 at 10:53 PM. Reason: Dviding the code

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The error occurs for the first of the reasons you suggest: index is <0. In particular the index is -1.

    The stack trace is saying that this is happening in the swapElements() method which is called from the sortArray() method.

    The first thing to do is figure out which of the index variables has the value -1. (then you can go on to see where it got that value). You can do this by inspecting sortArray() closely and thinking about it. Or you can use System.out.println():

    Java Code:
    //Swaps the values at two indices
    public static void swapElements(int[] x, int index1, int index2) {
        [color=blue]if(index1 < 0 || index2 < 0) {
            System.out.println("Bad index values! index1=" + index1 + " index2=" + index2);
        }[/color]
    
        // x[index1] gets overwritten, so it gets stored first
        int tempValue = x[index1];
    
        x[index1] = x[index2];
        x[index2] = tempValue;
    
    }

  3. #3
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You're doing a couple of things wrong in indexOfMaxInRange(). There is no point in giving "indexOfMaxInRange()" the "low" variable and you don't need a "value" variable. Your mistake was setting "index" to -1. What if it didn't find anything bigger than "value"? All you need is "i" to keep track of the current index and "index" to keep track of index of the biggest value in the array.

    Java Code:
    public static int indexOfMaxInRange(int numbers[], int beginIndex) {
        int i = beginIndex;
        int index = i;
        
        while(i < numbers.length) {
            if(numbers[i] > numbers[index])
                index = i;
        }
        
        return index;
    }
    Last edited by ra4king; 04-10-2011 at 03:58 AM.

  4. #4
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

Similar Threads

  1. ArrayIndexOutOfBoundsException: 0
    By mxsar in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 10:59 PM
  2. ArrayIndexOutOfBoundsException
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 06:50 PM
  3. ArrayIndexOutOfBoundsException
    By Corey in forum New To Java
    Replies: 5
    Last Post: 02-02-2010, 01:25 AM
  4. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 05:36 PM
  5. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM

Posting Permissions

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