How can I initialize an integer array in Java like so: int[] array = {1,2,3};
inside a switch statement?
Currently, I can write:
switch(something) {
case 0: int[] array = {1,2,3}; break;
default: int[] array = {3,2,1};
}
But when I try to access the array
variable, eclipse will complain that it might not be initialized.
If I try to declare it like int[] array;
or int[] array = new int[3];
and then have the switch statement, it would say I am trying to redeclare it.
How can I resolve this issue? Final idea is to be able to initialize an array with 10 values in just one line of code, based on some logic (a switch statement).
Put the declaration before the switch statement. You will also need to explicitly instantiate an array of the correct type.
int[] array;
switch (something) {
case 0: array = new int[] {1, 2, 3}; break;
default: array = new int[] {3, 2, 1};
}
I would tell you to put the array declaration outside the switch block, however, you cannot use = { 1, 2, 3}
syntax after the declaration. You need to initialize it the regular way, as in array = new int[] {1, 2, 3};
int[] array;
switch (something) {
case 0: array = new int[]{1, 2, 3}; break;
default: array = new int[]{3, 2, 1};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With