-1

This program is supposed to find Max and Min in an array, but it send error:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 23 at Mine_EnhancedForLoop.main(Mine_EnhancedForLoop.java:17)"
Does anyone know, what the problem is?

public class Mine_EnhancedForLoop {

    public static void main(String[] args) {

            int[] array1 = {23, 98, 10, 1, 45, 2, 7, 90};

            int max = array1[0];
            int min = array1[0];

            for (int i : array1){

                if (array1[i] > max)
                    max = array1[i];

                else if (array1[i] < min)
                    min = array1[i];
            }
            System.out.println("Maximum is: " + max);
            System.out.println("Minimum is: " + min);
    }
}
Hengameh
  • 774
  • 6
  • 12

6 Answers6

6

In for (int i : array1), i is a value contained in the array, not an index of the array. Therefore array1[i] will throw the exception you got whenever i >= array1.length (which is the case for most of the values in your input array).

If you don't need to know the index of the highest and lowest values, this will work :

        for (int i : array1) {
            if (i > max)
                max = i;
            else if (i < min)
                min = i;
        }
Eran
  • 359,724
  • 45
  • 626
  • 694
2
for (int i : array1)

i gives the element of array not the index of array

So in the first iteration

if(array1[23] > max)  //gave you ArrayIndexOutOfBoundsException 23

you can do

 int[] array1 = {23, 98, 10, 1, 45, 2, 7, 90};

 int max = array1[0];
 int min = array1[0];

 int i=0;

 while(i< array1.length){

      if (array1[i] > max)
           max = array1[i];

     else if (array1[i] < min)
           min = array1[i];

         i++;
      }
 System.out.println("Maximum is: " + max);
 System.out.println("Minimum is: " + min);

Demo

singhakash
  • 7,613
  • 4
  • 25
  • 59
1

You have to iterate the array elements as:

for (int i=0; i<array1.length; i++){
    if (array1[i] > max)
        max = array1[i];
    else if (array1[i] < min)
        min = array1[i];
}
Viswanath Donthi
  • 1,725
  • 1
  • 11
  • 12
1

Change the for loop to

for (int i = 0; i < array1.length; i++) {
    if (array1[i] > max) {
        max = array1[i];
    } else if (array1[i] < min)
        min = array1[i];
    }
}
Davide Lorenzo MARINO
  • 22,769
  • 4
  • 33
  • 48
1

Because variable i contains 23 when making the comparison here if (array1[i] > max), hence array1[23] gave the exception.

Loop over the array instead:

for (int i = 0; i < array1.length; i++) {
    if (array1[i] > max)
        max = array1[i];

    else if (array1[i] < min)
        min = array1[i];
}
abarisone
  • 3,429
  • 11
  • 27
  • 49
1

Actually it is much easier to use Java 8 streams:

int[] array1 = {23, 98, 10, 1, 45, 2, 7, 90};
System.out.println("Maximum is: " + Arrays.stream(array1).max().getAsInt());
System.out.println("Minimum is: " + Arrays.stream(array1).min().getAsInt());
Konrad Höffner
  • 8,510
  • 11
  • 50
  • 92