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