2

After reading, I came to know that, arrays in Java are objects. The name of the array is not the actual array, but just a reference. The new operator creates the array on the heap and returns the reference to the newly created array object which is then assigned to array variable (name). Something like the following:

int[] myArray = new int[5];

But I also used these two type of array declaration.

int[] myArray= new int[]{5,7,3};

and

int[] myArray= {5,7,3};

Both of the above are legal and work fine. So what's the difference between these two and when should I use them?

4

4 回答 4

3

请关注评论

int[] myArray = new int[5]; //memory allocated for 5 integers  with nulls as values


int[] myArray= new int[]{5,7,3}; //memory allocated for 3 integers  with  values


int[] myArray= {5,7,3}; // same as above with different syntax memory allocated for 3integers  with  values.

第二种和第三种风格的区别。

      someX(new int[] {1,2,3}); //  inline creation array  style
      someX(declaredArray);     // using some declared array
      someX({1,2,3});     //Error. Sorry boss, I don't know the type of array


      private  void someX(int[] param){
          // do something
      }
于 2013-10-24T06:05:49.867 回答
2

根据两种情况的字节码,没有区别。在这两种情况下,都分配了 3 个长度空间并分配了值。

int[] myArray=new int[]{5,7,3};

public class array.ArrayTest {
  public array.ArrayTest();
    Code:
       0: aload_0
       1: invokespecial #8                  //
()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_3
       1: newarray       int
       3: dup
       4: iconst_0
       5: iconst_5
       6: iastore
       7: dup
       8: iconst_1
       9: bipush        7
      11: iastore
      12: dup
      13: iconst_2
      14: iconst_3
      15: iastore
      16: astore_1
      17: return
}

int[] myArray= {5,7,3};

public class array.ArrayTest {
  public array.ArrayTest();
    Code:
       0: aload_0
       1: invokespecial #8                  //
()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_3
       1: newarray       int
       3: dup
       4: iconst_0
       5: iconst_5
       6: iastore
       7: dup
       8: iconst_1
       9: bipush        7
      11: iastore
      12: dup
      13: iconst_2
      14: iconst_3
      15: iastore
      16: astore_1
      17: return
}
于 2013-10-24T06:08:56.700 回答
1
int[] myArray = new int[5];

在此我们将长度 5 指定为数组

int[] myArray= new int[]{5,7,3};

在此我们传递三个元素,然后指定长度为 3。

int[] myArray= {5,7,3};

在这个我们传递的元素将自动指定长度为三。

于 2013-10-24T06:03:49.557 回答
1

这会生成一个大小为 5 的数组,其中包含 5 个空元素:

int[] myArray = new int[5];

如果这些值不是您在编译时知道的,这可能更有用。例如

int[] myArray = new int[blah.size()];
for (int i = 0; i < blah.size() ; i++) {
  myArray[i] = getFoo(blah.get(i));
}

如果您提前知道大小,则可以使用其他形式。

int[] myArray = {blah.get(0), blah.get(1), blah.get(2)};

以下是等价的(编译为相同的字节码),并生成一个推断大小为 3 的数组,其中包含三个元素 5、7 和 3。如果存在一组固定的值,或者至少是固定大小,则此形式很有用一组值。

int[] myArray = new int[]{5,7,3};
int[] myArray = {5,7,3};

否则你可以用更长的形式完成同样的事情:

int[] myArray = new int[5];
myArray[0] = 5;
myArray[1] = 7;
myArray[2] = 3;

但这是不必要的冗长。但如果你不知道有多少东西,你必须使用第一种形式。

于 2013-10-24T06:10:13.180 回答