Reputation: 93
I was recently assigned to create a bubble sort for a list of numbers (You'll find the list in the code I am about to show you), however, although the program compiles perfectly fine, I always get this run-time error in cmd...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at hw.main(hw.java:17)
Here is what I have so far.
//Ch 9 HW 7
class hw
{
public static void main(String[] args)
{
int[] x = new int[10];
x[0] = 100;
x[1] = 23;
x[2] = 15;
x[3] = 23;
x[4] = 7;
x[5] = 23;
x[6] = 2;
x[7] = 311;
x[8] = 5;
x[9] = 8;
x[10] = 3;
System.out.println(bubbleSort(x));
}
public static int[] bubbleSort(int[] x)
{
int placehold = 0;
for (int i = 0; i < x.length; i++)
{
if (x[i] > x[i + 1])
{
placehold = x[i + 1];
x[i] = x[i + 1];
x[i + 1] = placehold;
}
}
return x;
}
}
I was wondering if you guys could give me an explanation of the error and a way to fix it. Thanks a ton
~Andrew
EDIT: This code gives me a "cannot find symbol" error for "Arrays"
//Ch 9 HW 7
class hw
{
public static void main(String[] args)
{
int[] x = {100, 23, 15, 23, 7, 23, 2, 311, 5, 8, 3};
String y = Arrays.toString(bubbleSort(x));
System.out.println(y);
}
public static int[] bubbleSort(int[] x)
{
int placehold = 0;
for (int i = 0; i < x.length - 1; i++)
{
if (x[i] > x[i + 1])
{
placehold = x[i + 1];
x[i] = x[i + 1];
x[i + 1] = placehold;
}
}
return x;
}
}
FINAL EDIT W/ CORRECTIONS:
Here is the CORRECT code for those curious.
//Andrew Mancinelli
import java.util.*;
class hw
{
public static void main(String[] args)
{
int[] x = {100, 23, 15, 23, 7, 23, 2, 311, 5, 8, 3};
String y = Arrays.toString(bubbleSort(x));
System.out.println(y);
}
public static int[] bubbleSort(int[] x)
{
for (int start = 0; start < x.length - 1; start++)
{
int min = x[start];
int indexMin = start;
for (int j = start + 1; j < x.length; j++)
{
if (x[j] < min)
{
min = x[j];
indexMin = j;
}
}
x[indexMin] = x[start];
x[start] = min;
}
return x;
}
}
Upvotes: 1
Views: 518
Reputation: 124646
The array you created has capacity for 10 elements:
int[] x = new int[10];
If you look closely, you're assigning 11 elements:
x[0] = 100;
x[1] = 23;
x[2] = 15;
// ...
x[10] = 3;
So perhaps change the capacity to 11 when you declare it:
int[] x = new int[11];
But there's a much easier way to create that array:
int[] x = {100, 23, 15, 23, 7, 23, 2, 311, 5, 8, 3};
Notice that the size of the array is not specified here. As such, you cannot mistakenly give too small or too large size.
In this code, when i = x.length - 1
, the expression x[i + 1]
will be out of bounds:
for (int i = 0; i < x.length; i++)
{
if (x[i] > x[i + 1])
{
placehold = x[i + 1];
x[i] = x[i + 1];
x[i + 1] = placehold;
}
}
To fix that, change the loop condition so that i
doesn't reach x.length - 1
:
for (int i = 0; i < x.length - 1; i++)
Printing an int[]
with System.out.println
will not produce what you want.
This is what you want:
System.out.println(Arrays.toString(bubbleSort(x)));
Without the Arrays.toString(...)
, the value [I@6d06d69c
you saw is the hexadecimal representation of the hashcode of the array. This is the default implementation of Object.toString
, which arrays inherit.
It's pretty useless, the standard way to get a string representation of an array is using Arrays.toString(...)
.
Note: Arrays
is in java.util
, so you need to add an import for this:
import java.util.Arrays;
Upvotes: 2
Reputation: 21
In your loop the value of i goes from 0 to 10. so when i = 10, x[i+1] = x[11] but there is no 11th index in your array which is why you are getting the runtime exception called ArrayIndexOutOfBoundsException.
So you things you can get out of this problem is: 1) make sure you are not exceeding the array index when accessing the array 2) Use Error message as a clue to debug the error.
Upvotes: 0
Reputation: 172
The if condition of the BubbleSort method is asking for the index i+1 in the last iteration ask for a index which doesnt' exists that's why the exception is being thrown
Upvotes: 0