理解List.java的` T[] toArray(T[] a)`方法。

huangapple go评论56阅读模式
英文:

Understanding List.java's `<T> T[] toArray(T[] a)` method

问题

有一个 Java 列表,类型为 List&lt;int[]&gt; lst = new ArrayList&lt;&gt;();

我有以下代码片段,正在尝试解读。它们都与理解泛型和方法 toArray() 有关。

  1. lst.toArray(new int[0][]); - 这里的大小 0 不重要。试图理解为什么?我尝试查看 ArrayList 内部 toArray 方法的实现。

  2. lst.toArray(new int[lst.size()][]) - 类似于 (1),我可以使用 lst.size() 进行初始化。这是否意味着 (1) 和 (2) 之间没有概念上的区别?

  3. 鉴于 (1) 和 (2) 都可以正确工作,我尝试了这个 lst.toArray(new int[][]) - 这无法编译并显示 需要数组初始化程序。这与 Java 的语法有关吗?在实例化数组时,大小是否是强制性的,因此会出错?

有人能帮我理解这个吗?

英文:

Have a Java list as List&lt;int[]&gt; lst = new ArrayList&lt;&gt;();

I have the following code snippet's that I am trying to decipher. They all relate to understanding of generics and the the method toArray()

  1. lst.toArray(new int[0][]); - The size 0 does not matter here. Trying to understand why? I tried looking at the implementation of the toArray method inside ArrayList as well.

  2. lst.toArray(new int[lst.size()][]) - Similar to (1), I can init use lst.size(). Does it mean there is no conceptual difference between (1) and (2)

  3. Given (1) and (2) both work correctly, I tried this lst.toArray(new int[][]) - This does not compile and gives Array initializer expected. Is it related to the grammar of Java where if you are instantiating an array, size needs to be mandatory and hence the error?

Can someone help me understand this?

答案1

得分: 3

lst.toArray(new int[0][]); - 这里的大小为0并不重要。试图理解为什么?我尝试查看ArrayList内的toArray方法的实现。

从文档中可以得知:

...如果集合适合指定的数组,则返回该数组。否则,将分配一个具有指定数组的运行时类型和此集合大小的新数组。

任何小于lst.size()的大小都会导致分配一个新数组。

lst.toArray(new int[lst.size()][]) - 类似于(1),我可以使用lst.size()进行初始化。这是否意味着(1)和(2)之间没有概念上的区别?

基本上是的。唯一的区别是这里传入的数组对象将被使用,而不是新分配的数组。

鉴于(1)和(2)都能正常工作,我尝试了这个lst.toArray(new int[][]) - 这不会编译,并显示"Array initializer expected"。这是否与Java的语法有关,即如果您正在实例化一个数组,大小必须是强制性的,因此会出错?

这是正确的。

英文:

> lst.toArray(new int[0][]); - The size 0 does not matter here. Trying to understand why? I tried looking at the implementation of the toArray method inside ArrayList as well.

From the documentation:
> ...If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

Any size less than lst.size() will result in a new array being allocated.

> lst.toArray(new int[lst.size()][]) - Similar to (1), I can init use lst.size(). Does it mean there is no conceptual difference between (1) and (2)

Pretty much. The only difference is that the array object you passed in here will be used, not a newly allocated array.

> Given (1) and (2) both work correctly, I tried this lst.toArray(new int[][]) - This does not compile and gives Array initializer expected. Is it related to the grammar of Java where if you are instantiating an array, size needs to be mandatory and hence the error?

This is correct.

答案2

得分: 1

根据toArray(T[] a)的Java文档:

> 如果列表适合指定的数组,则将其返回。
> 否则,将分配一个新数组,其运行时类型为
> 指定的数组并且大小为此列表的大小。

因此,传递的数组大小会影响是否分配新数组。因此,在实践中,您可以要么传递大小为零的数组(在您的情况下是new int[0][]),要么传递大小大于等于lst.size()的数组。传递大小为正但小于列表大小的数组没有意义,因为它将被抛出以创建适合列表的新数组。通常,程序员更喜欢传递大小为零的数组,因为它简单而简洁。

关于第3点 - 您完全正确,Java要求在创建数组时定义数组大小。

英文:

According to toArray(T[] a) javadoc:

> If the list fits in the specified array, it is returned therein.
> Otherwise, a new array is allocated with the runtime type of the
> specified array and the size of this list.

So size of passed array affects would a new array be allocated or not. Thus in practice you are either pass zero sized array (new int[0][] in your case), either array of size >= to lst.size(). Passing an array of positive size, but less then list size makes no sense as it would be thrown to create a new array that fits the list. Usually programmers prefer to pass zero sized array as it easy and short.

Relating to 3) - you are absolutely right, Java requires array size to be defined when you create an array.

答案3

得分: 0

在Java中,你应该使用List<Integer>而不是int[]。尽管在Java中,int数组被识别为一个对象,因此将其作为类型提供也是可行的。

Collections Framework是为对象而设计的,而不是原始数据类型。

关于大小为0的问题,这里并不重要。需要理解的原因是什么?

基本上,编译器需要知道这个对象是什么,所以new int[0]就足够了。

这是否意味着(1)和(2)之间没有概念上的区别?

多多少少是的。

我建议阅读以下这本书,它很好地涵盖了Collections Framework、对象和数组的互操作性。
Java: The Complete Reference, Twelfth Edition, 12th Edition

英文:

Technically, you should be using a List&lt;Integer&gt;, and not an int[].
Although, in Java, an array of int is recognized as an object.&nbsp; Thus, providing it as the type will work.

The Collections Framework was made for use with objects, and not primitive data-types.

> "... The size 0 does not matter here. Trying to understand why? ..."

Essentially, the compiler needs to know what the object is&mdash;so new int[0] will suffice.

> "... Does it mean there is no conceptual difference between (1) and (2) ..."

More or less, yes.

> "... I tried this lst.toArray(new int[][]) ...
> ... Is it related to the grammar of Java where if you are instantiating an array, size needs to be mandatory and hence the error?"

Exactly, the syntax must comply.

I recommend the following book, which covers this topic well&mdash;the interoperability of the Collections Framework, objects, and arrays.
Java: The Complete Reference, Twelfth Edition, 12th Edition.

huangapple
  • 本文由 发表于 2023年6月16日 02:03:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定