toArray(T[] a) 和 toArray() 之间的区别
Difference between toArray(T[] a) and toArray()
我一直在学习如何使用 java 进行编程,但我对 LinkedList
的 toArray(T[] a)
和 toArray()
的区别没有任何明确的解释方法。第二个简单的 returns 将 LinkedList 对象内的所有元素作为一个数组,对吧?但是,第一个呢?
编辑:
我的意思是,我阅读了 oracle 的文档,它说:
Returns an array containing all of the elements in this list in proper
sequence (from first to last element); the runtime type of the
returned array is that of the specified array. 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. If the list fits in the specified array with room to spare
(i.e., the array has more elements than the list), the element in the
array immediately following the end of the list is set to null. (This
is useful in determining the length of the list only if the caller
knows that the list does not contain any null elements.)
Like the toArray() method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
我不明白加粗的句子的意思。
有两点不同:
- 第一个returns
T[]
而第二个returnsObject[]
- 第一个接受一个数组作为参数,如果这个数组足够大,它使用这个数组来存储Collection的元素,而不是创建一个新的。
假设您有一个 List<String>
,并且您想要将其转换为 String[]
。让我们看看两种方法的工作原理:
List<String> source = new LinkedList<String>();
// Fill in some data
Object[] array1 = source.toArray();
String[] array2 = source.toArray(new String[source.size()]);
看出区别了吗?第一个简单地创建一个 Object[]
,因为它不知道类型参数 <T>
的类型,而第二个只是填充你传递的 String[]
(这是你想)。您几乎总是需要使用第二种方法。
我一直在学习如何使用 java 进行编程,但我对 LinkedList
的 toArray(T[] a)
和 toArray()
的区别没有任何明确的解释方法。第二个简单的 returns 将 LinkedList 对象内的所有元素作为一个数组,对吧?但是,第一个呢?
编辑:
我的意思是,我阅读了 oracle 的文档,它说:
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. 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. If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
我不明白加粗的句子的意思。
有两点不同:
- 第一个returns
T[]
而第二个returnsObject[]
- 第一个接受一个数组作为参数,如果这个数组足够大,它使用这个数组来存储Collection的元素,而不是创建一个新的。
假设您有一个 List<String>
,并且您想要将其转换为 String[]
。让我们看看两种方法的工作原理:
List<String> source = new LinkedList<String>();
// Fill in some data
Object[] array1 = source.toArray();
String[] array2 = source.toArray(new String[source.size()]);
看出区别了吗?第一个简单地创建一个 Object[]
,因为它不知道类型参数 <T>
的类型,而第二个只是填充你传递的 String[]
(这是你想)。您几乎总是需要使用第二种方法。