如何在 Java 中初始化数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1938101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize an array in Java?
提问by chatty
I am initializing an array like this:
我正在初始化一个这样的数组:
public class Array {
int data[] = new int[10];
/** Creates a new instance of Array */
public Array() {
data[10] = {10,20,30,40,50,60,71,80,90,91};
}
}
NetBeans points to an error at this line:
NetBeans 指出这一行的错误:
data[10] = {10,20,30,40,50,60,71,80,90,91};
How can I solve the problem?
我该如何解决问题?
采纳答案by Prasoon Saurav
data[10] = {10,20,30,40,50,60,71,80,90,91};
The above is not correct (syntax error). It means you are assigning an array to data[10]
which can hold just an element.
以上不正确(语法错误)。这意味着您正在分配一个data[10]
只能容纳一个元素的数组。
If you want to initialize an array, try using Array Initializer:
如果要初始化数组,请尝试使用Array Initializer:
int[] data = {10,20,30,40,50,60,71,80,90,91};
// or
int[] data;
data = new int[] {10,20,30,40,50,60,71,80,90,91};
Notice the difference between the two declarations. When assigning a new array to a declared variable, new
must be used.
注意这两个声明之间的区别。将新数组分配给已声明的变量时,new
必须使用。
Even if you correct the syntax, accessing data[10]
is still incorrect (You can only access data[0]
to data[9]
because index of arrays in Java is 0-based). Accessing data[10]
will throw an ArrayIndexOutOfBoundsException.
即使您更正了语法,访问data[10]
仍然是不正确的(您只能访问data[0]
,data[9]
因为 Java 中的数组索引是从 0 开始的)。访问data[10]
将抛出ArrayIndexOutOfBoundsException。
回答by Dean Povey
Try data = new int[] {10,20,30,40,50,60,71,80,90,91 };
尝试 data = new int[] {10,20,30,40,50,60,71,80,90,91 };
回答by schubySteve
you are trying to set the 10th element of the array to the array try
您正在尝试将数组的第 10 个元素设置为数组尝试
data = new int[] {10,20,30,40,50,60,71,80,90,91};
FTFY
FTFY
回答by fastcodejava
You cannot initialize an array like that. In addition to what others have suggested, you can do :
你不能像那样初始化一个数组。除了其他人的建议之外,您还可以执行以下操作:
data[0] = 10;
data[1] = 20;
...
data[9] = 91;
回答by Bernie Perez
When you create an array of size 10 it allocated 10 slots but from 0 to 9. This for loop might help you see that a little better.
当您创建一个大小为 10 的数组时,它分配了 10 个插槽,但从 0 到 9。这个 for 循环可能会帮助您更好地了解这一点。
public class Array {
int[] data = new int[10];
/** Creates a new instance of an int Array */
public Array() {
for(int i = 0; i < data.length; i++) {
data[i] = i*10;
}
}
}
回答by Vinayak
Syntax
句法
Datatype[] variable = new Datatype[] { value1,value2.... }
Datatype variable[] = new Datatype[] { value1,value2.... }
Example :
例子 :
int [] points = new int[]{ 1,2,3,4 };
回答by Vinayak
You can do:
你可以做:
int[] data = {10,20,30,40,50,60,71,80,90,91};
回答by Sajjad Behravesh
Maybe this will work:
也许这会奏效:
public class Array {
int data[] = new int[10];
/* Creates a new instance of Array */
public Array() {
data= {10,20,30,40,50,60,71,80,90,91};
}
}
回答by Shiva
Rather than learning un-Official websites learn from oracle website
与其学习非官方网站,不如学习oracle网站
link follows:Click here
链接如下:点击这里
*You can find Initialization as well as declaration with full description *
*您可以找到带有完整描述的初始化和声明*
int n; // size of array here 10
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Integer.parseInt(s.nextLine()); // using Scanner class
}
Input: 10//array size 10 20 30 40 50 60 71 80 90 91
输入:10//数组大小10 20 30 40 50 60 71 80 90 91
Displaying data:
显示数据:
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i] + " ");
}
Output:10 20 30 40 50 60 71 80 90 91
输出:10 20 30 40 50 60 71 80 90 91
回答by Sedat Kilinc
If you want to initialize an array in a constructor, you can't use those array initializer like.
如果你想在构造函数中初始化一个数组,你不能像这样使用那些数组初始化器。
data= {10,20,30,40,50,60,71,80,90,91};
Just change it to
只需将其更改为
data = new int[] {10,20,30,40,50,60,71,80,90,91};
You don't have to specify the size with data[10] = new int[] { 10,...,91}
Just declare the property / field with int[] data;
and initialize it like above.
The corrected version of your code would look like the following:
您不必指定大小,data[10] = new int[] { 10,...,91}
只需int[] data;
像上面一样声明属性/字段并对其进行初始化。更正后的代码版本如下所示:
public class Array {
int[] data;
public Array() {
data = new int[] {10,20,30,40,50,60,71,80,90,91};
}
}
As you see the bracket are empty. There isn't any need to tell the size between the brackets, because the initialization and its size are specified by the count of the elements between the curly brackets.
如您所见,括号是空的。不需要告诉括号之间的大小,因为初始化及其大小是由大括号之间的元素数指定的。