The compiler produces an error:

The indexMin and indexMax variables may not be defined.

Although the mechanism for their definition, although written inside if, it works with any variant of filling the array. How to fix?

/** * * my third homework * */ import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main(String[]args) { // гСнСрация массива int[] MyArr = new int[15]; for(int i = 0; i< MyArr.length; i++) { MyArr[i] = (int) Math.round((Math.random() * 30) - 15); System.out.println(MyArr[i]); } int s = 0; // Π½Π°Ρ‡Π°Π»ΠΎ области int f = MyArr.length - 1; //ΠΊΠΎΠ½Π΅Ρ† области int indexMin, indexMax; // Ρ‚ΡƒΡ‚ Π±ΡƒΠ΄ΡƒΡ‚ хранится индСксы минимального ΠΈ максимального значСния Π² области int min = 16, max = -16; while (s<= f && f>= s) { // поиск Π½Π°ΠΈΠΌ ΠΈ Π½Π°ΠΈΠ± Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ Π² области ΠΈ запись ΠΈΡ… индСксов for(int i = 0; i< MyArr.length; i++) { if(MyArr[i] < min) { min = MyArr[i]; indexMin = i; } if(MyArr[i] > max) { max = MyArr[i]; indexMax = i; } } // пСрСстановка Π·Π°Π½Ρ‡Π΅Π½ΠΈΠΉ Π² Π½Π°Ρ‡Π°Π»ΠΎ ΠΈ ΠΊΠΎΠ½Π΅Ρ† области MyArr[indexMin] = MyArr[s]; MyArr[s] = min; MyArr[indexMax] = MyArr[f]; MyArr[f] = max; // сброс Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌΠ° ΠΈ максимума для поиска Π² Π½ΠΎΠ²ΠΎΠΉ области (этого ΠΌΠΎΠΆΠ½ΠΎ ΠΈ Π½Π΅ Π΄Π΅Π»Π°Ρ‚ΡŒ, Ρ‚.ΠΊ. ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ ΠΈ послСдний элСмСнты массива ΡƒΠΆΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΈ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ, просто пСрСстраховка) min = 16; max = -16; // суТСниС области s++; f--; } System.out.println("послС сортировки:"); for(int i = 0; i< MyArr.length; i++) { System.out.println(MyArr[i]); } } } 

    3 answers 3

    Set the initial value of your indexMin and indexMax here:

     int indexMin, indexMax; 

    Although they will be equal to 0, because you yourself did not explicitly state the values, the compiler will tell you that your variables may not be initialized because of if.

    • This is not true. Variable values ​​will not be equal to 0. Local variables are not initialized to zero values, they must be initialized explicitly. - m. vokhm February
    • @m. Vokhm, Similarly, I did not notice that they are local, but this does not change the essence, they need to be initialized. - Vennic

    If each value of your array is> = 16 or each value is <= -16, then one of the variables you have will not be initialized guaranteed.

    From the fact that you initialize them with a zero value, nothing bad will happen - within the loop, your logic will quickly replace them with the necessary ones.

      The compiler does not track the possible variants of execution of your code. If a variable is initialized inside a conditional operator, and then used outside this operator, then the compiler assumes that the condition can be both true and false, which means that under some conditions the variable may be uninitialized. The use of uninitialized local variables in java prohibited, so the compiler does not compile such code. Instance variables and class variables (that is, those described not inside the method) take zero values ​​by default and can be used without explicit initialization, but this does not apply to local variables (that is, those described inside methods); they should always be initialized explicitly. So give them some initial values, for example.

        int indexMin = Integer.MAX_VALUE, indexMax = Integer.MIN_VALUE;