java.lang.ArrayIndexOutOfBoundsException 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17109128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:01:38  来源:igfitidea点击:

java.lang.ArrayIndexOutOfBoundsException Error

javaarraysindexoutofboundsexception

提问by user2482380

How do I fix this error and what does it mean?

我该如何解决这个错误,它是什么意思?

java.lang.ArrayIndexOutOfBoundsException: 5
    at Sort.sort(Sort.java:29)
    at Sort.<init>(Sort.java:13)
    at SortedArray.<init>(SortedArray.java:23)

Here is the code:

这是代码:

import java.util.Scanner;
import java.util.Random;

public class SortedArray
{
    Scanner input = new Scanner(System.in);
    int [] Array;
    Sort sortedArray;
    int sizeOfArray;

    public SortedArray()
    {
        System.out.print("Enter the number of values to put in the array: ");
        sizeOfArray = input.nextInt();
        Array = new int [sizeOfArray];
        System.out.println("");
        for(int i = 0; i < sizeOfArray; i++)
        {
            Random r = new Random();
            Array[i] = r.nextInt(100) + 1;
            System.out.println(Array[i]);
        }
        sortedArray = new Sort(Array, sizeOfArray);
        sortedArray.display();
    } 
}


public class Sort
{
  int[] array;
  int sizeOfArray;
  public Sort(int[] oldArray, int sizeOfOldArray)
  {
    sizeOfArray = sizeOfOldArray;
    array = new int [sizeOfArray];
    for( int i = 0; i < sizeOfArray; i++)
    {
        array[i] = oldArray[i];
    }
    sort();
  }

  public void display()
    { 
       for ( int i = 0; i < sizeOfArray; i++){
           System.out.println(array[i]);
       }
    }

  private void sort()
    {
        for (int i = 0; i < sizeOfArray; i++)
        {
            for (int j = 0; j < sizeOfArray; i++)
            {
                if (array[j] < array[i])
                {
                    swap(i,j);
                }
            }
        }
    }

    private void swap(int x, int y)
    {
        int temp;
        temp = array[x];
        array[x] = array[y];
        array[y] = temp;
  }
}

I get the error when I run the program and enter the value. The program is supposed to sort the numbers from greatest to least. I'm not sure what is wrong.

运行程序并输入值时出现错误。该程序应该从最大到最小对数字进行排序。我不确定出了什么问题。

回答by SJuan76

First, what it means: you have an array and are trying to acces an index that is outside its range (below 0 or bigger or equal than the length of the array).

首先,这意味着什么:您有一个数组,并试图访问其范围之外的索引(低于 0 或大于或等于数组的长度)。

The probable cause is:

可能的原因是:

for (int j = 0; j < sizeOfArray; i++)

Notice that you check that jdoes not get too big but you are increasing i.

请注意,您检查j并没有变得太大,但您正在增加i

回答by PrR3

The Problem is that the inner loop also incrementsiwhich isn't correct in this situation!

问题是内部循环也会增加i,这在这种情况下是不正确的!

private void sort()
{
    for (int i = 0; i < sizeOfArray; i++)
    {
        for (int j = 0; j < sizeOfArray; j++)
        {
            if (array[j] < array[i])
            {
                swap(i,j);
            }
        }
    }
}

回答by Stochastically

Your problem is on this line

你的问题在这条线上

for (int j = 0; j < sizeOfArray; i++)

it should be

它应该是

for (int j = 0; j < sizeOfArray; j++)