为什么需要LinkedList的toArray(T[] a)方法中的变量'result'?
Why is the variable 'result' in the method toArray(T[] a) of LinkedList needed?
我目前正在尝试了解 JDK 中 LinkedList 的实现,我在方法中无意中发现了以下内容
toArray(T[] a)
所以在第1101行我不知道为什么要创建变量result
。由于 LinkedList 和 Array 中的项目是 T
我们不能在没有 result
的情况下在下面的 for 循环中这样做吗?
for (Node<E> x = first; x != null; x = x.next)
a[i++] = x.item;
如果不是,为什么会这样?
这是为了类型安全。在调用 toArray 之前创建数组时,您可以控制数组的类型。如果让 toArray 创建结果,它总是一个 Object 数组。
这应该说明如何使用 toArray:
public void example ()
{
List<String> data = new ArrayList<> ();
data.add ("alpha");
data.add ("beta");
data.add ("gamma");
Object[] arrayObjectData = data.toArray ();
// Elements of the array have type "Object"
Object o = arrayObjectData[0];
System.out.printf ("Object from array: %s %n", o);
String[] arrayStringData = new String[data.size ()];
data.toArray (arrayStringData);
// Elements of the array have type "String"
String s = arrayStringData[0];
System.out.printf ("String from array: %s %n", s);
}
您必须确保列表的内容与数组类型兼容。这将引发异常:
public void badExample ()
{
// List of Object
List<Object> data = new ArrayList<> ();
data.add ("alpha");
data.add ("beta");
data.add ("gamma");
// Object that can't be stored in an array of String
data.add (Boolean.TRUE);
Object[] arrayObjectData = data.toArray ();
// Elements of the array have type "Object"
Object o = arrayObjectData[0];
System.out.printf ("Object from array: %s %n", o);
String[] arrayStringData = new String[data.size ()];
data.toArray (arrayStringData);
// Elements of the array have type "String"
String s = arrayStringData[0];
System.out.printf ("String from array: %s %n", s);
}
我目前正在尝试了解 JDK 中 LinkedList 的实现,我在方法中无意中发现了以下内容
toArray(T[] a)
所以在第1101行我不知道为什么要创建变量result
。由于 LinkedList 和 Array 中的项目是 T
我们不能在没有 result
的情况下在下面的 for 循环中这样做吗?
for (Node<E> x = first; x != null; x = x.next)
a[i++] = x.item;
如果不是,为什么会这样?
这是为了类型安全。在调用 toArray 之前创建数组时,您可以控制数组的类型。如果让 toArray 创建结果,它总是一个 Object 数组。
这应该说明如何使用 toArray:
public void example ()
{
List<String> data = new ArrayList<> ();
data.add ("alpha");
data.add ("beta");
data.add ("gamma");
Object[] arrayObjectData = data.toArray ();
// Elements of the array have type "Object"
Object o = arrayObjectData[0];
System.out.printf ("Object from array: %s %n", o);
String[] arrayStringData = new String[data.size ()];
data.toArray (arrayStringData);
// Elements of the array have type "String"
String s = arrayStringData[0];
System.out.printf ("String from array: %s %n", s);
}
您必须确保列表的内容与数组类型兼容。这将引发异常:
public void badExample ()
{
// List of Object
List<Object> data = new ArrayList<> ();
data.add ("alpha");
data.add ("beta");
data.add ("gamma");
// Object that can't be stored in an array of String
data.add (Boolean.TRUE);
Object[] arrayObjectData = data.toArray ();
// Elements of the array have type "Object"
Object o = arrayObjectData[0];
System.out.printf ("Object from array: %s %n", o);
String[] arrayStringData = new String[data.size ()];
data.toArray (arrayStringData);
// Elements of the array have type "String"
String s = arrayStringData[0];
System.out.printf ("String from array: %s %n", s);
}