如何在Java中初始化数组?

时间:2021-05-10 21:30:08

I am initializing an array data 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 an error at the line

NetBeans在行中指向错误

data[10] = {10,20,30,40,50,60,71,80,90,91};

How can I solve the problem?

我如何解决这个问题?

10 个解决方案

#1


467  

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.

上面的错误(语法错误)。这意味着要为数据[10]分配一个数组,该数组只能包含一个元素。

If you want to initialize an array, try using 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.

注意这两个声明之间的差异。在将新数组分配给已声明变量时,必须使用新数组。

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.

即使您修改了语法,访问数据[10]仍然是不正确的(您只能访问数据[0]到数据[9],因为Java中的数组索引是基于0的)。访问数据[10]将抛出ArrayIndexOutOfBoundsException。

#2


27  

Try data = new int[] {10,20,30,40,50,60,71,80,90,91 };

Try data = new int[]{10、20、30、40、50、60、71、80、90、91};

#3


19  

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

#4


14  

You can do:

你能做什么:

int[] data = {10,20,30,40,50,60,71,80,90,91};

#5


10  

Syntax

语法

 Datatype[] variable = new Datatype[] { value1,value2.... }

 Datatype variable[]  = new Datatype[] { value1,value2.... }

Example :

例子:

int [] points = new int[]{ 1,2,3,4 };

#6


6  

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 30 40 50 60 71 80 90 91

Displaying data:

显示数据:

for(int y=0;y<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

#7


3  

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;

#8


1  

you are trying to set the 10th element of the array to the array try

您正在尝试将数组的第10个元素设置为数组try

data = new int[] {10,20,30,40,50,60,71,80,90,91};

FTFY

FTFY

#9


1  

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:

您不必使用数据[10]= new int[]{10,…,91}使用int[]数据声明属性/字段;像上面那样初始化它。更正后的代码如下:

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.

当你看到括号是空的。不需要告诉括号之间的大小,因为初始化及其大小是由花括号之间的元素计数指定的。

#10


0  

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

#1


467  

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.

上面的错误(语法错误)。这意味着要为数据[10]分配一个数组,该数组只能包含一个元素。

If you want to initialize an array, try using 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.

注意这两个声明之间的差异。在将新数组分配给已声明变量时,必须使用新数组。

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.

即使您修改了语法,访问数据[10]仍然是不正确的(您只能访问数据[0]到数据[9],因为Java中的数组索引是基于0的)。访问数据[10]将抛出ArrayIndexOutOfBoundsException。

#2


27  

Try data = new int[] {10,20,30,40,50,60,71,80,90,91 };

Try data = new int[]{10、20、30、40、50、60、71、80、90、91};

#3


19  

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

#4


14  

You can do:

你能做什么:

int[] data = {10,20,30,40,50,60,71,80,90,91};

#5


10  

Syntax

语法

 Datatype[] variable = new Datatype[] { value1,value2.... }

 Datatype variable[]  = new Datatype[] { value1,value2.... }

Example :

例子:

int [] points = new int[]{ 1,2,3,4 };

#6


6  

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 30 40 50 60 71 80 90 91

Displaying data:

显示数据:

for(int y=0;y<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

#7


3  

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;

#8


1  

you are trying to set the 10th element of the array to the array try

您正在尝试将数组的第10个元素设置为数组try

data = new int[] {10,20,30,40,50,60,71,80,90,91};

FTFY

FTFY

#9


1  

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:

您不必使用数据[10]= new int[]{10,…,91}使用int[]数据声明属性/字段;像上面那样初始化它。更正后的代码如下:

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.

当你看到括号是空的。不需要告诉括号之间的大小,因为初始化及其大小是由花括号之间的元素计数指定的。

#10


0  

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