使用 toArray() 方法将 ArrayList 转换为 Array
Transforming an ArrayList to an Array using toArray() method
考虑以下代码:
import java.util.*;
public class ArrayQuestion{
public static void main(String[] args){
List<String> list = new ArrayList<>();
list.add("Chocolate");
list.add("Vanilla");
//Converting the List to an Array
Object[] objectArray = list.toArray();
所以 toArray 方法 return 是一个默认类型 Object[] 的数组。
假设我想创建一个 String 数组,我读到我会将一个 string[] 对象传递给 toArray 方法。
String[] stringArray = list.toArray(new String[0]);
我读到为参数指定大小 0 的优点是 Java 将 为 return 值创建一个适当大小的新数组.
有人可以解释一下吗,我在Java API 中查找了toArray(String[] stringArray) 方法。我仍然不明白上面的说法也暗指什么 return 值。
我的问题具体是关于传递给 toArray 方法的参数,为什么它是 0 以及传递 0 如何创建一个适当大小的列表数组。
列表的 return 值将分配给 stringArray
。例如,如果 list
包含 15 个字符串,那么调用 list.toArray(new String[0])
将 return 具有 15 个元素的 String[]
。
这里引用了更多细节,直接来自 javadoc:
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 collection 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.)
当您将太小的数组传递给 toArray 方法时,它会创建一个 class 相同但大小正确的数组。空数组(长度为 0)非常适合。
查看 LinkedList 等的实现
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
行为应该从那里很清楚。
考虑以下代码:
import java.util.*;
public class ArrayQuestion{
public static void main(String[] args){
List<String> list = new ArrayList<>();
list.add("Chocolate");
list.add("Vanilla");
//Converting the List to an Array
Object[] objectArray = list.toArray();
所以 toArray 方法 return 是一个默认类型 Object[] 的数组。 假设我想创建一个 String 数组,我读到我会将一个 string[] 对象传递给 toArray 方法。
String[] stringArray = list.toArray(new String[0]);
我读到为参数指定大小 0 的优点是 Java 将 为 return 值创建一个适当大小的新数组.
有人可以解释一下吗,我在Java API 中查找了toArray(String[] stringArray) 方法。我仍然不明白上面的说法也暗指什么 return 值。
我的问题具体是关于传递给 toArray 方法的参数,为什么它是 0 以及传递 0 如何创建一个适当大小的列表数组。
列表的 return 值将分配给 stringArray
。例如,如果 list
包含 15 个字符串,那么调用 list.toArray(new String[0])
将 return 具有 15 个元素的 String[]
。
这里引用了更多细节,直接来自 javadoc:
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 collection 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.)
当您将太小的数组传递给 toArray 方法时,它会创建一个 class 相同但大小正确的数组。空数组(长度为 0)非常适合。
查看 LinkedList 等的实现
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
行为应该从那里很清楚。