java中的数组定义:创建数组时有以下2种方法有什么区别? [英] Array definition in java : what is the difference between following 2 approaches in creating an array?

查看:66
本文介绍了java中的数组定义:创建数组时有以下2种方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建数组时遵循以下2种方法有何区别?

根据用途,哪个是首选?



方法1:





int a [] = new int [5];

a [0] = 98;

a [1] = 97;

a [2] = 99;

a [3] = 94;

a [4] = 96;



方法2:

int a [] = {98,97,99,94,96};



我尝试了什么:



我试过两种方式想知道哪一种方式是否更好基于使用?

解决方案

据我所知应该没有区别。然而,在我看来,方法2看起来更好看。


为什么不亲自尝试看看?在第一种方法中,您将创建数组并使用相同的值初始化它。



在第一种方法中,您创建的array-int是一种基本类型,因此它们不会为空,并且您最终会创建对象并默认为零。试试这段代码,

  int  [] array =  new   int  [ 5 ];  //  默认为0  

System.out.println(array [< span class =code-digit> 3 ]); // 存在第4个对象; 0。
// 输出
// 0

第二种方法既可读也适用于JVM,在此,你创建数组并使用默认值对其进行初始化。

 int [] array = {1,2,3,4,5}; 

System.out.println(array [3]);
//输出
// 4

这意味着JVM为您设置了一些东西。为简单起见,它们都是相同的,

  int  a; 
a = 5 ;

//
int a = 5 ;

哪一个更好?如果您在申报时不知道价值,那么第一个是好的,如果您知道,则第二个更好。第二种方法是冗余调用(根据编译器),如果你还必须接受输入并在下一次调用中设置它。我把这个选择留在你手中。



在我看来,转到第二种方法,除非你从每个元素的用户那里获取输入,然后初始化数组并从中读取输入是有意义的一条小溪。除此之外,我总是很欣赏有第二种方法的代码。



你可以在网上玩这个,http://tpcg.io/wPt4ZI


What is the difference between following 2 approaches in creating an array.
Which is preferred depending on the use?

Approach 1:


int a[]= new int[5];
a[0] = 98;
a[1] = 97;
a[2] = 99;
a[3] = 94;
a[4] = 96;

Approach 2:
int a[]={98,97,99,94,96};

What I have tried:

I tried both the ways bu want to know which one way is better based on the use?

解决方案

There should be no difference as far as I know. Yet approach 2 is better looking, in my opinion.


Why don't you try and see for yourself? In the first approach, you are creating the array and initializing it with the values in the same go.

In the first approach, you are creating the array—int is a primitive type, thus they are not null-ed, and you end up with object creation and defaulted to zero. Try this code,

int[] array = new int[5]; // Defaults to 0
        
System.out.println(array[3]); // 4th object exists; 0.
// Outputs 
// 0

The second approach is both, readable and good for the JVM, in this, you create the array and initialize it with the default values.

int[] array =  { 1, 2, 3, 4, 5 };
        
System.out.println(array[3]);
// Outputs 
// 4

This means that JVM setup things for you. To make things simple, they are both the same as,

int a;
a = 5;

// And
int a = 5;

Which one is better? First one is good if you do not know the value at the declaration time, and second is better if you know. Second approach would be redundant call (as per compiler), if you also have to take the input and set it in the next call. I leave that choice in your hand.

In my opinion, go for the second approach, unless you are taking input from the users for each element then it makes sense to initialize the array and read the input from a stream. Other than that, I would always appreciate the code that has second approach.

You can play with this online, http://tpcg.io/wPt4ZI.


这篇关于java中的数组定义:创建数组时有以下2种方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆