我只是在查看 List 接口(interface)中定义的方法:<T> T[] toArray(T[] a)
,我有一个问题。为什么是通用的?由于这个事实,方法不是完全类型安全的。以下代码片段编译但会导致 ArrayStoreException
:
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
String[] stringArray = list.toArray(new String[]{});
在我看来,如果 toArray 不是泛型并采用 List 类型参数,那就更好了。
我已经编写了玩具示例,没有通用的也可以:
package test;
import java.util.Arrays;
public class TestGenerics<E> {
private Object[] elementData = new Object[10];
private int size = 0;
public void add(E e) {
elementData[size++] = e;
}
@SuppressWarnings("unchecked")
//I took this code from ArrayList but it is not generic
public E[] toArray(E[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (E[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
public static void main(String[] args) {
TestGenerics<Integer> list = new TestGenerics<Integer>();
list.add(1);
list.add(2);
list.add(3);
//You don't have to do any casting
Integer[] n = new Integer[10];
n = list.toArray(n);
}
}
有什么理由这样声明吗?
最佳答案
来自 javadocs :
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.
这意味着程序员可以控制它应该是什么类型的数组。
例如,对于您的 ArrayList<Integer>
而不是 Integer[]
你可能想要一个 Number[]
的数组或 Object[]
数组。
此外,该方法还检查传入的数组。如果传入的数组对所有元素都有足够的空间,toArray
方法重用该数组。这意味着:
Integer[] myArray = new Integer[myList.size()];
myList.toArray(myArray);
或
Integer[] myArray = myList.toArray(new Integer[myList.size()]);
效果和
一样Integer[] myArray = myList.toArray(new Integer[0]);
注意,在旧版本的 Java 中,后一种操作使用反射来检查数组类型,然后动态构造正确类型的数组。通过首先传入正确大小的数组,不必使用反射在 toArray
内分配新数组。方法。现在不再是这种情况了,两个版本可以互换使用。
关于Java List <T> T[] toArray(T[] a) 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422161/