I have tried to initialize an array dynamically and display the array as result:
我试图动态初始化一个数组并显示数组结果:
import java.util.Scanner;
public class Arrays {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int[] c;
String cmd="yes";
while(cmd=="yes") {
System.out.println("Enter value for c :");
c=new int[] {in.nextInt()};
System.out.println("Continue(yes/no)? :");
cmd=in.next();
}
for(int k:c)
System.out.println(k);
}
}
But at the for-each loop(i.e. line 12 from main()) it is showing error as "The local variable c may not have been initialized".
但是在for-each循环(即main()的第12行)中,它显示错误为“局部变量c可能尚未初始化”。
2 个解决方案
#1
2
The other answers are correct, you did not initialize your array. you would need to call c=null;
for this to work.
其他答案都是正确的,你没有初始化你的数组。你需要调用c = null;为了这个工作。
However, if I understand correctly, you are trying to add numbers to an array of indefinite size, which you cannot do in Java. you should use an ArrayList.
但是,如果我理解正确,那么您正在尝试将数字添加到不确定大小的数组中,这是Java无法做到的。你应该使用ArrayList。
you also used == to compare String values, which is a big no-no, I have changed it to .equals
in my answer below.
你还使用==来比较字符串值,这是一个很大的禁忌,我在下面的答案中将它改为.equals。
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
ArrayList<Integer> c = new ArrayList<>();
String cmd="yes";
while(cmd.equals("yes")) {
System.out.println("Enter value for c :");
c.add((Integer)in.nextInt());
System.out.println("Continue(yes/no)? :");
cmd=in.next();
}
for(Integer k:c)
System.out.println(k);
}
#2
0
For your actual compilation error, c
could be not initialized if the loop is not executed. Even if you know it will (since cmd
is equals to "yes"
, the compiler don't go that far).
对于实际的编译错误,如果未执行循环,则c可能无法初始化。即使你知道它会(因为cmd等于“是”,编译器也不会那么远)。
You will find answers that initialize at first the value, like Neng Liu's answer, but based on your logic, you can use a do..while
loop since your logic show that you want to read at least once the Scanner
.
您会找到最初初始化值的答案,如Neng Liu的答案,但根据您的逻辑,您可以使用do..while循环,因为您的逻辑显示您想要至少读取一次扫描程序。
do{
System.out.println("Enter value for c :");
c=new int[] {in.nextInt()};
System.out.println("Continue(yes/no)? :");
cmd=in.next();
}while("yes".equals(cmd)); //.equals is needed to compare `String` correctly
The condition in a do..while
is checked after the block statement is executed (compare to a while
that check the condition before). So the statement will be execute AT LEAST ONCE, so you are sure c
will be initialized that way. And the compiler knows that too.
在执行块语句之后检查do..while中的条件(比较之前检查条件的while)。因此该语句将至少执行,因此您确定c将以这种方式初始化。编译器也知道这一点。
Since the title mention you want a the dynamic sized array, see HamishD's answer to use a Collection
that will grow on needs. And for a more complete post about List
, see Java dynamic array sizes?
由于标题提到你想要一个动态大小的数组,请参阅HamishD的答案,使用一个将根据需求增长的Collection。有关List的更完整帖子,请参阅Java动态数组大小?
#1
2
The other answers are correct, you did not initialize your array. you would need to call c=null;
for this to work.
其他答案都是正确的,你没有初始化你的数组。你需要调用c = null;为了这个工作。
However, if I understand correctly, you are trying to add numbers to an array of indefinite size, which you cannot do in Java. you should use an ArrayList.
但是,如果我理解正确,那么您正在尝试将数字添加到不确定大小的数组中,这是Java无法做到的。你应该使用ArrayList。
you also used == to compare String values, which is a big no-no, I have changed it to .equals
in my answer below.
你还使用==来比较字符串值,这是一个很大的禁忌,我在下面的答案中将它改为.equals。
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
ArrayList<Integer> c = new ArrayList<>();
String cmd="yes";
while(cmd.equals("yes")) {
System.out.println("Enter value for c :");
c.add((Integer)in.nextInt());
System.out.println("Continue(yes/no)? :");
cmd=in.next();
}
for(Integer k:c)
System.out.println(k);
}
#2
0
For your actual compilation error, c
could be not initialized if the loop is not executed. Even if you know it will (since cmd
is equals to "yes"
, the compiler don't go that far).
对于实际的编译错误,如果未执行循环,则c可能无法初始化。即使你知道它会(因为cmd等于“是”,编译器也不会那么远)。
You will find answers that initialize at first the value, like Neng Liu's answer, but based on your logic, you can use a do..while
loop since your logic show that you want to read at least once the Scanner
.
您会找到最初初始化值的答案,如Neng Liu的答案,但根据您的逻辑,您可以使用do..while循环,因为您的逻辑显示您想要至少读取一次扫描程序。
do{
System.out.println("Enter value for c :");
c=new int[] {in.nextInt()};
System.out.println("Continue(yes/no)? :");
cmd=in.next();
}while("yes".equals(cmd)); //.equals is needed to compare `String` correctly
The condition in a do..while
is checked after the block statement is executed (compare to a while
that check the condition before). So the statement will be execute AT LEAST ONCE, so you are sure c
will be initialized that way. And the compiler knows that too.
在执行块语句之后检查do..while中的条件(比较之前检查条件的while)。因此该语句将至少执行,因此您确定c将以这种方式初始化。编译器也知道这一点。
Since the title mention you want a the dynamic sized array, see HamishD's answer to use a Collection
that will grow on needs. And for a more complete post about List
, see Java dynamic array sizes?
由于标题提到你想要一个动态大小的数组,请参阅HamishD的答案,使用一个将根据需求增长的Collection。有关List的更完整帖子,请参阅Java动态数组大小?