Java在构造函数中初始化int数组

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

I have a class and in that class I have this:

我有一节课,在那节课里,我有:

 //some code
 private int[] data = new int[3];
 //some code

Then in my constructor:

然后在我的构造函数:

public Date(){
    data[0] = 0;
    data[1] = 0;
    data[2] = 0;
}

If I do this, everything is OK. Default data values are initialized but if I instead do this:

如果我这样做,一切都没问题。初始化默认数据值,但是如果我这样做:

public Date(){
    int[] data = {0,0,0};
}

It says:

它说:

Local variable hides a field

Why?

为什么?

Whats the best way to initialize an array inside the constructor?

在构造函数中初始化数组的最佳方法是什么?

thanks

谢谢

6 个解决方案

#1


132  

private int[] data = new int[3];

This already initializes your array elements to 0. You don't need to repeat that again in the constructor.

这已经将数组元素初始化为0。您不需要在构造函数中再次重复此操作。

In your constructor it should be:

在您的构造函数中应该是:

data = new int[]{0, 0, 0};

#2


6  

You could either do:

你可以做的:

public class Data {
    private int[] data;

    public Data() {
        data = new int[]{0, 0, 0};
    }
}

Which initializes data in the constructor, or:

在构造函数中初始化数据的,或:

public class Data {
    private int[] data = new int[]{0, 0, 0};

    public Data() {
        // data already initialised
    }
}

Which initializes data before the code in the constructor is executed.

在执行构造函数中的代码之前初始化数据。

#3


4  

This is because, in the constructor, you declared a local variable with the same name as an attribute.

这是因为在构造函数中,您声明了一个与属性同名的局部变量。

To allocate an integer array which all elements are initialized to zero, write this in the constructor:

要分配一个所有元素都初始化为零的整数数组,请在构造函数中写入:

data = new int[3];

To allocate an integer array which has other initial values, put this code in the constructor:

若要分配具有其他初始值的整数数组,请将此代码放入构造函数:

int[] temp = {2, 3, 7};
data = temp;

or:

或者:

data = new int[] {2, 3, 7};

#4


4  

why not simply

为什么不简单

public Date(){
    data = new int[]{0,0,0};
}

the reason you got the error is because int[] data = ... declares a new variable and hides the field data

您得到错误的原因是int[] data =…声明一个新变量并隐藏字段数据

however it should be noted that the contents of the array are already initialized to 0 (the default value of int)

但是,应该注意数组的内容已经初始化为0 (int的默认值)

#5


0  

in your constructor you are creating another int array:

在构造函数中,您正在创建另一个int数组:

 public Date(){
  int[] data = {0,0,0};
  }

Try this:

试试这个:

 data = {0,0,0};

NOTE: By the way you do NOT need to initialize your array elements if it is declared as an instance variable. Instance variables automatically get their default values, which for an integer array, the default values are all zeroes.

注意:顺便说一下,如果数组元素被声明为实例变量,则不需要初始化数组元素。实例变量自动获得它们的默认值,对于整数数组,默认值都是0。

If you had locally declared array though they you would need to initialize each element.

如果你有本地声明的数组,你需要初始化每个元素。

#6


0  

The best way is not to write any initializing statements. This is because if you write int a[]=new int[3] then by default, in Java all the values of array i.e. a[0], a[1] and a[2] are initialized to 0! Regarding the local variable hiding a field, post your entire code for us to come to conclusion.

最好的方法是不编写任何初始化语句。这是因为如果您写入int a[]=new int[3],那么在Java中,所有的数组值,例如[0]、[1]和[2],都被初始化为0!关于隐藏字段的局部变量,请发布您的全部代码,以便我们得出结论。

#1


132  

private int[] data = new int[3];

This already initializes your array elements to 0. You don't need to repeat that again in the constructor.

这已经将数组元素初始化为0。您不需要在构造函数中再次重复此操作。

In your constructor it should be:

在您的构造函数中应该是:

data = new int[]{0, 0, 0};

#2


6  

You could either do:

你可以做的:

public class Data {
    private int[] data;

    public Data() {
        data = new int[]{0, 0, 0};
    }
}

Which initializes data in the constructor, or:

在构造函数中初始化数据的,或:

public class Data {
    private int[] data = new int[]{0, 0, 0};

    public Data() {
        // data already initialised
    }
}

Which initializes data before the code in the constructor is executed.

在执行构造函数中的代码之前初始化数据。

#3


4  

This is because, in the constructor, you declared a local variable with the same name as an attribute.

这是因为在构造函数中,您声明了一个与属性同名的局部变量。

To allocate an integer array which all elements are initialized to zero, write this in the constructor:

要分配一个所有元素都初始化为零的整数数组,请在构造函数中写入:

data = new int[3];

To allocate an integer array which has other initial values, put this code in the constructor:

若要分配具有其他初始值的整数数组,请将此代码放入构造函数:

int[] temp = {2, 3, 7};
data = temp;

or:

或者:

data = new int[] {2, 3, 7};

#4


4  

why not simply

为什么不简单

public Date(){
    data = new int[]{0,0,0};
}

the reason you got the error is because int[] data = ... declares a new variable and hides the field data

您得到错误的原因是int[] data =…声明一个新变量并隐藏字段数据

however it should be noted that the contents of the array are already initialized to 0 (the default value of int)

但是,应该注意数组的内容已经初始化为0 (int的默认值)

#5


0  

in your constructor you are creating another int array:

在构造函数中,您正在创建另一个int数组:

 public Date(){
  int[] data = {0,0,0};
  }

Try this:

试试这个:

 data = {0,0,0};

NOTE: By the way you do NOT need to initialize your array elements if it is declared as an instance variable. Instance variables automatically get their default values, which for an integer array, the default values are all zeroes.

注意:顺便说一下,如果数组元素被声明为实例变量,则不需要初始化数组元素。实例变量自动获得它们的默认值,对于整数数组,默认值都是0。

If you had locally declared array though they you would need to initialize each element.

如果你有本地声明的数组,你需要初始化每个元素。

#6


0  

The best way is not to write any initializing statements. This is because if you write int a[]=new int[3] then by default, in Java all the values of array i.e. a[0], a[1] and a[2] are initialized to 0! Regarding the local variable hiding a field, post your entire code for us to come to conclusion.

最好的方法是不编写任何初始化语句。这是因为如果您写入int a[]=new int[3],那么在Java中,所有的数组值,例如[0]、[1]和[2],都被初始化为0!关于隐藏字段的局部变量,请发布您的全部代码,以便我们得出结论。