Minimizing the scope of a variable in do ... while loop [Java] - ERROR: Character cannot be found

I would like to reduce the area arrto a loop do ... while, so it can be destroyed after the loop exits.

If I declare arrin a loop do while, I get an error:

character not found

I can declare this right before the loop, but then I keep the bloated arrin my memory, although I no longer need it.

//int[] arr = {0}; // this works, but the scope is outside the loop
do {
    int[] arr = {0};  // causes "cannot find symbol" on `while` line
    arr = evolve(arr); // modifies array, saves relevant results elsewhere
    } while (arr.length < 9999);

What is the right way to deal with this situation?

+4
source share
5 answers

You have:

  • The initial state
  • Final state
  • Iterative operation

, for ( ):

for (int[] arr = {0}; arr.length < 9999; arr = evolve(arr));

, , :

arr = null;

- . , .

+6

. boolean cicle . , do while.

+2

@Bohemian .

, , do.. while,

    int l = 9999;
    do {
        int[] arr = { 0 }; // causes "cannot find symbol" on `while` line
        arr = evolve(arr); // modifies array, saves relevant results elsewhere
        l = arr.length;
    } while (l < 9999);
+2

do-while . , . .

+2

- , , , , - :

{
    int[] arr = {0}; // this works, but the scope is outside the loop
    do {
        int[] arr = {0};
        arr = evolve(arr); // modifies array, saves relevant results elsewhere
    } while (arr.length < 9999);
}

@martinhh - .

, , , , , . , , .

+1

Source: https://habr.com/ru/post/1650208/


All Articles