Java:如何在switch案例中初始化int数组?

时间:2022-01-06 21:29:52

How can I initialize an integer array in Java like so: int[] array = {1,2,3}; inside a switch statement?

如何在Java中初始化整数数组,如下所示:int [] array = {1,2,3};在switch语句中?

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.

但是当我尝试访问数组变量时,eclipse会抱怨它可能没有被初始化。

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.

如果我尝试将其声明为int [] array;或int [] array = new int [3];然后有switch语句,它会说我正在尝试重新声明它。

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).

我该如何解决这个问题?最后的想法是能够基于某些逻辑(switch语句)在一行代码中初始化一个包含10个值的数组。

4 个解决方案

#1


14  

Put the declaration before the switch statement. You will also need to explicitly instantiate an array of the correct type.

在switch语句之前放置声明。您还需要显式实例化正确类型的数组。

int[] array;
switch (something) {
    case 0: array = new int[] {1, 2, 3}; break;
    default: array = new int[] {3, 2, 1};
}

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

我会告诉你把数组声明放在switch块之外,但是,声明后你不能使用= {1,2,3}语法。您需要以常规方式初始化它,如在array = new int [] {1,2,3}中;

#3


1  

int[] array;
switch (something) {
    case 0: array = new int[]{1, 2, 3}; break;
    default: array = new int[]{3, 2, 1};
}

#4


0  

I had the same question before. Hope this helps you.

我之前有同样的问题。希望这对你有所帮助。

First Mark Byers is absolutely right. Java works with minimal possible scope. so if you try to do

第一马克拜尔斯是绝对正确的。 Java以尽可能小的范围工作。所以,如果你试图这样做

case 1: int arr = whatever and case 2 : int arr = ... it will give you an error of re-declaration because entire switch is treated as a single scope and there is no case wise / based scope.

case 1:int arr = whatever和case 2:int arr = ...它会给你一个重新声明的错误,因为整个开关被视为一个范围而且没有基于case / based的范围。

Point 2: if you say:

第2点:如果你说:

case 1: int arr = whatever and case 2 : arr = ... assuming that int arr will be compiled and taken from case 1 then you are still wrong and again java will give you the error, based on the principle that each and every local var has to be declared and initialized before its use.

情况1:int arr = what和case 2:arr = ...假设int arr将被编译并从案例1中获取然后你仍然是错误的并且java将根据每个原则提供错误必须在使用之前声明和初始化local var。

so the best way is to declare it outside your switch and define or initialize it in whichever case you prefer.

因此,最好的方法是在交换机外面声明它,并在您喜欢的任何情况下定义或初始化它。

#1


14  

Put the declaration before the switch statement. You will also need to explicitly instantiate an array of the correct type.

在switch语句之前放置声明。您还需要显式实例化正确类型的数组。

int[] array;
switch (something) {
    case 0: array = new int[] {1, 2, 3}; break;
    default: array = new int[] {3, 2, 1};
}

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

我会告诉你把数组声明放在switch块之外,但是,声明后你不能使用= {1,2,3}语法。您需要以常规方式初始化它,如在array = new int [] {1,2,3}中;

#3


1  

int[] array;
switch (something) {
    case 0: array = new int[]{1, 2, 3}; break;
    default: array = new int[]{3, 2, 1};
}

#4


0  

I had the same question before. Hope this helps you.

我之前有同样的问题。希望这对你有所帮助。

First Mark Byers is absolutely right. Java works with minimal possible scope. so if you try to do

第一马克拜尔斯是绝对正确的。 Java以尽可能小的范围工作。所以,如果你试图这样做

case 1: int arr = whatever and case 2 : int arr = ... it will give you an error of re-declaration because entire switch is treated as a single scope and there is no case wise / based scope.

case 1:int arr = whatever和case 2:int arr = ...它会给你一个重新声明的错误,因为整个开关被视为一个范围而且没有基于case / based的范围。

Point 2: if you say:

第2点:如果你说:

case 1: int arr = whatever and case 2 : arr = ... assuming that int arr will be compiled and taken from case 1 then you are still wrong and again java will give you the error, based on the principle that each and every local var has to be declared and initialized before its use.

情况1:int arr = what和case 2:arr = ...假设int arr将被编译并从案例1中获取然后你仍然是错误的并且java将根据每个原则提供错误必须在使用之前声明和初始化local var。

so the best way is to declare it outside your switch and define or initialize it in whichever case you prefer.

因此,最好的方法是在交换机外面声明它,并在您喜欢的任何情况下定义或初始化它。