0

I looked over on stackoverflow but I still can't figure what's wrong with the code. PS: I am a beginner /****************************************************************/

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

}

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

The error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 at Main.main(Main.java:17) exit status 1

padurean04
  • 27
  • 5

1 Answers1

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

x should less than SIZE instead of <= since array begin with index 0 and end with (size-1)

Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29