简体   繁体   中英

Run-time error when handling arrays and a bubble sort (Java)

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;
    }   
}

Bug #1

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.

Bug #2

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++)

Bug 3

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;

BubbleSort方法的if条件在上次迭代中要求索引i + 1要求不存在的索引,这就是引发异常的原因

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM