Results 1 to 4 of 4
- 04-09-2011, 10:26 PM #1
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)):
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.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
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; }
Thanks for your help!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---------------"); }Last edited by Boreeas; 04-09-2011 at 10:53 PM. Reason: Dviding the code
- 04-09-2011, 11:04 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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; }
- 04-10-2011, 03:55 AM #3
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.
- 04-10-2011, 08:28 PM #4
Similar Threads
-
ArrayIndexOutOfBoundsException: 0
By mxsar in forum New To JavaReplies: 3Last Post: 11-16-2010, 10:59 PM -
ArrayIndexOutOfBoundsException
By er1c550n20 in forum New To JavaReplies: 2Last Post: 04-07-2010, 06:50 PM -
ArrayIndexOutOfBoundsException
By Corey in forum New To JavaReplies: 5Last Post: 02-02-2010, 01:25 AM -
ArrayIndexOutOfBoundsException
By flaskvacuum in forum New To JavaReplies: 6Last Post: 07-14-2009, 05:36 PM -
ArrayIndexOutofBoundsException help
By filly444 in forum New To JavaReplies: 9Last Post: 09-03-2008, 05:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks