赋值 - Java数组初始化,这三种方式有什么区别?
天蓬老师
天蓬老师 2017-04-18 09:49:39
0
4
936

形式1:

Object[] params = null;
params = {1, 2, 3, 4};

形式2:

Object[] params = {1, 2, 3, 4};

形式3:

Object[] params = new Object[]{1, 2, 3, 4};

1.为什么第一种形式就是错的(编译阶段就报错),而第二种就可以呢?(第三种很显然)
2.第二种和第三种,哪种初始化方式更优呢?(或者有更好的?)

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(4)
迷茫

First of all, the length of the array must be determined when using array initialization, which means that the length of the array is immutable.
There are two ways to create an array in JAVA:
(1) Static creation such as

Object[] arr1 = {"a","b","c"}; 

(2)Dynamic creation like
1

    Object[] arr3 = new Object[]{"a","b","c"};

2

    Object[] arr2 = new Object[3];  
            arr2[1] = "a";
            arr2[2] = "b";
            arr2[3] = "c";
            //先确认元素个数,一般情况下习惯使用动态创建方式 比较灵活 可以先规定元素个数 后对每个元素进行赋值

3

    Object[] arr4 = null;  
    arr4 = new Object[3];
            arr4[1] = "a";
            arr4[2] = "b";
            arr4[3] = "c";
            //直接给arr4初始化为null,然后要使用的时候再创建一个新的数组new Object[3],让arr4指向新数组的地址,然后再依次赋值

And the method mentioned by the questioner:

Object[] params = null;
params = {1, 2, 3, 4};

Error 1: You are directly creating an empty array params, which means it is empty. The length of the array cannot be changed. If you add data to it at this time, an empty array cannot add anything naturally, so an error will occur during compilation. . You should do the same as the second method I mentioned above, re-create an array with a length of 4, and give the address of the new array to params, that is, Object[] params = null; params = new Object[4]; this way Can be created.
Error 2: The initialization of the array can only be added continuously when it is defined. After the definition is completed

Object[] params = null;
params = new Object[4];

After that, if you want to add data to it, you can only add it one by one by subscripting the array.

params[1] = 1;
params[2] = 2;
params[23 = 3;
params[4] = 4;

Instead of directly params = {1, 2, 3, 4};

迷茫

Form 1

Object[] params = {1, 2, 3, 4};

This declaration method is actually just syntax sugar for Java to initialize the array. The so-called syntax sugar is just for the convenience of programmers. In actual execution, Java will turn it into form 3, which is

Object[] params = new Object[]{1, 2, 3, 4};

Java does not provide syntactic sugar similar to form 2, so it is illegal to use it this way.

迷茫
Object[] params = null;
params = new Object[]{1, 2, 3, 4};

A. The author does not need to worry too much about the syntax stipulations
B. If you know the array elements in advance, it is more concise to use Object[] params = {1, 2, 3, 4}; to declare the array code. There is no better way to say it.

巴扎黑

The first way of declaration is incorrect
It should be Object[] params = new Object[length]

The second one is what I usually use

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!