错误java.lang.ArrayIndexOutOfBoundsException

时间:2018-10-23 09:38:03

标签: java arrays exception indexoutofboundsexception

我查看了stackoverflow,但是我仍然无法弄清楚代码出了什么问题。 PS:我是初学者 / ***************************************************** *************** /

class Main 
{
  public static void main(String[] args) 
  {
    int SIZE = 10;
    int[] sortedArray = new int[SIZE];
    int[] intArray = new int[]{ 5,2,10,4,1,6,99,8,9,1 };
    int x=0;
    int y=0;
    // Random big nunbver to ensure no number in array is bigger
    int biggestNumberFound = 10000000;
    int maxValue=0;
    for ( x = 0; x <= SIZE; x++) 
      for ( y = 0; y <= SIZE; y++)
        maxValue = 0;
        if (intArray[y] > maxValue && intArray[y] < biggestNumberFound)
          maxValue = intArray[y];
      sortedArray[x] = maxValue;
      biggestNumberFound = maxValue;
    System.out.println(sortedArray);
  }

}

/ ************************************************** * /

错误: 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:11     在Main.main(Main.java:17) 退出状态1

1 个答案:

答案 0 :(得分:0)

for ( x = 0; x <= SIZE; x++) 

x应该小于SIZE而不是<=,因为数组以索引0开头并以(size-1)结尾