ArrayList类的toArray()方法 (ArrayList Class toArray() method)
Syntax:
句法:
public Object[] toArray();
public T[] toArray(T[] elements);
toArray() method is available in java.util package.
toArray()方法在java.util包中可用。
toArray() method is used to convert the given Arraylist to an array or in other words, this method is used to return an array that contains all the elements in this Arraylist.
toArray()方法用于将给定的Arraylist转换为数组,换句话说,该方法用于返回包含该Arraylist中所有元素的数组。
toArray(T[] elements) method is used to return an array of the runtime type is that of the given array T[], when this Arraylist fits in the given array then the same array will be returned else a new array is allocated is the type of the given array.
toArray(T [] elements)方法用于返回运行时类型为给定数组T []的数组,当此Arraylist适合给定数组时,将返回同一数组,否则分配新数组给定数组的类型。
toArray() method does not throw an exception at the time of returning an array.
toArray()方法在返回数组时不会引发异常。
toArray(T[] elements) method may throw an exception at the time of returning an array.
toArray(T [] elements)方法可能会在返回数组时引发异常。
- ArrayStoreException: This exception may throw when the dynamic type of the given array T[] is not a parent type of the dynamic type of element in this Arraylist.ArrayStoreException :如果给定数组T []的动态类型不是此Arraylist中元素的动态类型的父类型,则可能引发此异常。
- NullPointerException: This exception may throw when the given array is null exists.NullPointerException :当给定数组为null时,可能引发此异常。
These are non-static methods, it is accessible with the class object and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象进行访问,如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, toArray(): It does not accept any parameter.
在第一种情况下, toArray() :它不接受任何参数。
In the Second case, toArray(T[] elements):
在第二种情况下, toArray(T [] elements) :
T[] elements – represents the array to store elements, when it is capable to store else it creates a new array according to its size of the same dynamic type.
T [] elements –表示要存储元素的数组,当它能够存储其他元素时,它将根据相同动态类型的大小创建一个新数组。
Return value:
返回值:
In the first case, The return type of the method is Object(), it returns an array of Object type that contains all the elements in this Arraylist.
在第一种情况下,该方法的返回类型为Object() ,它返回一个Object类型的数组,其中包含此Arraylist中的所有元素。
In the second case, The return type of the method is T[], it returns an array that contains all the elements of this array.
在第二种情况下,该方法的返回类型为T [] ,它返回一个包含该数组所有元素的数组。
Example:
例:
// Java program to demonstrate the example
// of void toArray() method of ArrayList
import java.util.*;
public class ToArrayOfArrayList {
public static void main(String args[]) {
// Create an ArrayList with initial capacity
// to store elements
ArrayList < String > arr_l = new ArrayList < String > (10);
String str_l[] = new String[4];
// By using add() method is to add elements
// in the ArrayList
arr_l.add("C");
arr_l.add("C++");
arr_l.add("Java");
arr_l.add("DotNet");
// Display ArrayList
System.out.println("ArrayList Elements :" + arr_l);
System.out.println();
// Display String Array
for (String s: str_l)
System.out.println("str_l :" + s);
// By using toArray() method is to convert the
// collection to Array
Object[] o = arr_l.toArray();
System.out.println();
// Display ArrayList
for (Object val: arr_l)
System.out.println("arr_l.toArray() : " + val);
// By using toArray(T[]) method is to coipies the
// collection to the given Array
str_l = arr_l.toArray(str_l);
System.out.println();
// Display str_l
for (String val1: str_l)
System.out.println("arr_l.toArray(str_l) : " + val1);
}
}
Output
输出量
ArrayList Elements :[C, C++, Java, DotNet]str_l :null
str_l :null
str_l :null
str_l :nullarr_l.toArray() : C
arr_l.toArray() : C++
arr_l.toArray() : Java
arr_l.toArray() : DotNetarr_l.toArray(str_l) : C
arr_l.toArray(str_l) : C++
arr_l.toArray(str_l) : Java
arr_l.toArray(str_l) : DotNet
翻译自: https://www.includehelp.com/java/arraylist-toarray-method-with-example.aspx