java中new String[]{}和new String[]的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20396182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:38:40  来源:igfitidea点击:

difference between new String[]{} and new String[] in java

javaarraysstring

提问by

I am fresherin java ,i have a doubt in java that is

我对 Java 比较新鲜,我对 Java 有疑问

String array= new String[]{};
  1. what is the use of { }here ?
  2. what is the difference between String array=new String[];and String array=new String[]{};
  3. when I am writing String array=new String[10]{};got error why? Help me I am confused.
  1. 什么是使用{}在这里?
  2. 之间有什么区别String array=new String[];String array=new String[]{};
  3. 我写的时候String array=new String[10]{};出错了,为什么?帮我弄糊涂了。

采纳答案by Vimal Bera

String array=new String[];and String array=new String[]{};both are invalid statement in java.

String array=new String[];并且String array=new String[]{};两者都是java中的无效语句。

It will gives you an error that you are trying to assign String arrayto Stringdatatype. More specifically error is like this Type mismatch: cannot convert from String[] to String

它会给你一个你试图分配String arrayString数据类型的错误。更具体地说,错误是这样的Type mismatch: cannot convert from String[] to String

回答by Ross Drew

{}defines the contents of the array, in this case it is empty. These would both have an array of three Strings

{}定义数组的内容,在这种情况下它是空的。这些都将有一个由三个Strings 组成的数组

String[] array = {"element1","element2","element3"};
String[] array = new String[] {"element1","element2","element3"};

while []on the definition side (right side of =) of a statement defines the size of an intended array, e.g. this would have an array of 10 locations to place Strings

[]=语句的定义侧(右侧)定义了预期数组的大小,例如,这将有一个包含 10 个位置的数组来放置Strings

String[] array = new String[10];

...But...

...但...

String array = new String[10]{};  //The line you mentioned above

Was wrong because you are defining an array of length 10 ([10]), then defining an array of length 0 ({}), and trying to set them to the same array reference (array) in one statement. Both cannot be set.

是错误的,因为您定义了一个长度为 10 ( [10]) 的数组,然后定义了一个长度为 0 ( {})的数组,并试图array在一个语句中将它们设置为相同的数组引用 ( )。两者都不能设置。

Additionally

此外

The array should be defined as an array of a given type at the start of the statement like String[] array. String array = /* array value*/is saying, set an array value to a String, not to an array of Strings.

该数组应在语句的开头定义为给定类型的数组,例如String[] array. String array = /* array value*/就是说,将数组值设置为 a String,而不是设置为Strings的数组。

回答by SpringLearner

String array[]=new String[]; and String array[]=new String[]{};

No difference,these are just different ways of declaring array

没有区别,这些只是声明数组的不同方式

String array=new String[10]{}; got error why ?

字符串数组=新字符串[10]{};为什么出错?

This is because you can not declare the size of the array in this format.

这是因为您不能以这种格式声明数组的大小。

right way is

正确的方法是

String array[]=new String[]{"a","b"};

回答by Prabhakaran Ramaswamy

Try this one.

试试这个。

  String[] array1= new String[]{};
  System.out.println(array1.length);
  String[] array2= new String[0];
  System.out.println(array2.length);

Note: there is no byte code difference between new String[]{}; and new String[0];

注意:new String[]{} 之间没有字节码区别;和新的字符串[0];

new String[]{}is array initialization with values.

new String[]{}是带值的数组初始化。

new String[0];is array declaration(only allocating memory)

new String[0];是数组声明(只分配内存)

new String[10]{};is not allowed because new String[10]{ may be here 100 values};

new String[10]{};不允许,因为 new String[10]{ may be here 100 values};

回答by Dawood ibn Kareem

You have a choice, when you create an object array (as opposed to an array of primitives).

当您创建对象数组(而不是原始数组)时,您有一个选择。

One option is to specify a size for the array, in which case it will just contain lots of nulls.

一种选择是指定数组的大小,在这种情况下,它将只包含大量空值。

String[] array = new String[10]; // Array of size 10, filled with nulls.

The other option is to specify what will be in the array.

另一个选项是指定数组中的内容。

String[] array = new String[] {"Larry", "Curly", "Moe"};  // Array of size 3, filled with stooges.

But you can't mix the two syntaxes. Pick one or the other.

但是您不能混合使用这两种语法。选择其中之一。

回答by TWiStErRob

TL;DR

TL; 博士

  • An array variable has to be typed T[]
    (note that T can be an arry type itself -> multidimensional arrays)
  • The length of the array must be determined either by:
    • giving it an explicit size
      (can be intconstant or intexpression, see nbelow)
    • initializing all the values inside the array
      (length is implicitly calculated from given elements)
  • Any variable that is typed T[]has one read-only field: lengthand an index operator [int]for reading/writing data at certain indices.
  • 必须键入数组变量T[]
    (请注意,T 本身可以是 arry 类型 -> 多维数组)
  • 数组的长度必须由以下任一确定:
    • 给它一个明确的大小
      (可以是int常量或int表达式,见n下文)
    • 初始化数组内的所有值
      (长度是根据给定元素隐式计算的)
  • 任何类型化的变量T[]都有一个只读字段:length和一个索引运算符,[int]用于在某些索引处读取/写入数据。

Replies

回复

1. String[] array= new String[]{};what is the use of { } here ?

1.String[] array= new String[]{};这里的{}有什么用?

It initializes the array with the values between { }. In this case 0 elements, so array.length == 0and array[0] throws IndexOutOfBoundsException: 0.

它用 之间的值初始化数组{ }。在这种情况下 0 个元素,所以array.length == 0array[0] throws IndexOutOfBoundsException: 0

2. what is the diff between String array=new String[];and String array=new String[]{};

2.什么是之间的差异String array=new String[];String array=new String[]{};

The first won't compile for two reasons while the second won't compile for one reason. The common reason is that the type of the variable array has to be an array type: String[]not just String. Ignoring that (probably just a typo) the difference is:

第一个不会编译有两个原因,而第二个不会编译有一个原因。常见的原因是变量数组的类型必须是数组类型:String[]而不仅仅是String. 忽略(可能只是一个错字)的区别是:

new String[]   // size not known, compile error
new String[]{} // size is known, it has 0 elements, listed inside {}
new String[0]  // size is known, it has 0 elements, explicitly sized

3. when am writing String array=new String[10]{};got error why ?

3. 写的时候String array=new String[10]{};出错是什么原因?

(Again, ignoring the missing []before array) In this case you're over-eager to tell Java what to do and you're giving conflicting data. First you tell Java that you want 10 elements for the array to hold and then you're saying you want the array to be empty via {}. Just make up your mind and use one of those - Java thinks.

(同样,忽略[]之前的缺失array)在这种情况下,您过于急于告诉 Java 该怎么做,并且您提供了相互矛盾的数据。首先,您告诉 Java 您希望数组包含 10 个元素,然后您说希望通过{}. 只要下定决心并使用其中之一 - Java 认为。

help me i am confused

帮帮我,我很困惑

Examples

例子

String[] noStrings = new String[0];
String[] noStrings = new String[] { };
String[] oneString = new String[] { "atIndex0" };
String[] oneString = new String[1];
String[] oneString = new String[] { null }; // same as previous
String[] threeStrings = new String[] { "atIndex0", "atIndex1", "atIndex2" };
String[] threeStrings = new String[] { "atIndex0", null, "atIndex2" }; // you can skip an index
String[] threeStrings = new String[3];
String[] threeStrings = new String[] { null, null, null }; // same as previous
int[] twoNumbers = new int[2];
int[] twoNumbers = new int[] { 0, 0 }; // same as above
int[] twoNumbers = new int[] { 1, 2 }; // twoNumbers.length == 2 && twoNumbers[0] == 1 && twoNumbers[1] == 2
int n = 2;
int[] nNumbers = new int[n]; // same as [2] and { 0, 0 }
int[] nNumbers = new int[2*n]; // same as new int[4] if n == 2

(Here, "same as" means it will construct the same array.)

(这里,“same as”意味着它将构造相同的数组。)

回答by Aamir Kalimi

1.THE USE OF {}:

1.{} 的使用:

It initialize the array with the values { }

它用值 { } 初始化数组

2.The difference between String array=new String[]; and String array=new String[]{};

2.String array=new String[]的区别;和字符串数组=新字符串[]{};

String array=new String[]; and String array=new String[]{}; both are invalid statement in java.

It will gives you an error that you are trying to assign String array to String datatype. More specifically error is like this Type mismatch: cannot convert from String[] to String

字符串数组=新字符串[]; 和字符串数组=新字符串[]{};两者都是java中的无效语句。

它会给您一个错误,提示您尝试将 String 数组分配给 String 数据类型。更具体地说,错误是这样的 Type mismatch: cannot convert from String[] to String

3.String array=new String[10]{}; got error why?

3.字符串数组=新字符串[10]{};有错误为什么?

Wrong because you are defining an array of length 10 ([10]), then defining an array of length String[10]{}0

错误,因为你定义了一个长度为 10 ([10]) 的数组,然后定义了一个长度为String[10]的数组{}0