Java中AbstractSequentialList類的toArray(arr [])方法方法用於生成與AbstractSequentialList相同元素的數組。它以正確的順序返回一個數組,其中包含此AbstractSequentialList中的所有元素。返回數組的運行時類型是指定數組的運行時類型。如果AbstractSequentialList適合指定的數組,則在其中返回它。否則,將使用指定數組的運行時類型和此AbstractSequentialList的大小分配一個新數組。如果AbstractSequentialList適合指定的數組並有剩餘空間(即,數組中的元素比AbstractSequentialList多),則將緊接AbstractSequentialList末尾的數組中的元素設置為null。 (僅當調用者知道AbstractSequentialList不包含任何null元素時,這才對確定AbstractSequentialList的長度很有用。)
用法:
public <T> T[] toArray(T[] a)
參數:該方法接受一個參數arr [](如果該參數足夠大),該參數是將AbstractSequentialList的元素存儲到的數組;否則,將為此分配一個具有相同運行時類型的新數組。
返回值:該方法返回一個包含與AbstractSequentialList相似的元素的數組。
異常:該方法可能會引發兩種類型的異常:
- ArrayStoreException:當提到的數組具有不同的類型並且無法與AbstractSequentialList中提到的元素進行比較時:
- NullPointerException :如果數組為Null,則拋出此異常。
下麵的程序演示了AbstractSequentialList.toArray(arr [])方法的用法。
程序1:當數組的大小為AbstractSequentialList時
// Java code to illustrate toArray(arr[])
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
// Creating an empty AbstractSequentialList
AbstractSequentialList<String>
abs_col = new LinkedList<String>();
// Use add() method to add
// elements into the AbstractSequentialList
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// Displaying the AbstractSequentialList
System.out.println("The AbstractSequentialList:"
+ abs_col);
// Creating the array and using toArray()
String[] arr = new String[5];
arr = abs_col.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
The AbstractSequentialList:[Welcome, To, Geeks, For, Geeks] The arr[] is: Welcome To Geeks For Geeks
程序2:當數組小於AbstractSequentialList的大小時
// Java code to illustrate toArray(arr[])
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
// Creating an empty AbstractSequentialList
AbstractSequentialList<String>
abs_col = new LinkedList<String>();
// Use add() method to add
// elements into the AbstractSequentialList
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// Displaying the AbstractSequentialList
System.out.println("The AbstractSequentialList:"
+ abs_col);
// Creating the array and using toArray()
String[] arr = new String[1];
arr = abs_col.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
The AbstractSequentialList:[Welcome, To, Geeks, For, Geeks] The arr[] is: Welcome To Geeks For Geeks
程序3:當數組大於AbstractSequentialList的大小時
// Java code to illustrate toArray(arr[])
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
// Creating an empty AbstractSequentialList
AbstractSequentialList<String>
abs_col = new LinkedList<String>();
// Use add() method to add
// elements into the AbstractSequentialList
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// Displaying the AbstractSequentialList
System.out.println("The AbstractSequentialList:"
+ abs_col);
// Creating the array and using toArray()
String[] arr = new String[10];
arr = abs_col.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
The AbstractSequentialList:[Welcome, To, Geeks, For, Geeks] The arr[] is: Welcome To Geeks For Geeks null null null null null
程序4:演示NullPointerException
// Java code to illustrate toArray(arr[])
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
// Creating an empty AbstractSequentialList
AbstractSequentialList<String>
abs_col = new LinkedList<String>();
// Use add() method to add
// elements into the AbstractSequentialList
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// Displaying the AbstractSequentialList
System.out.println("The AbstractSequentialList:"
+ abs_col);
try {
// Creating the array
String[] arr = null;
// using toArray()
// Since arr is null
// Hence exception will be thrown
arr = abs_col.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
The AbstractSequentialList:[Welcome, To, Geeks, For, Geeks] Exception:java.lang.NullPointerException
相關用法
- Java AbstractSequentialList contains()用法及代碼示例
- Java AbstractSequentialList.subList()用法及代碼示例
- Java AbstractSequentialList hashCode()用法及代碼示例
- Java AbstractSequentialList iterator()用法及代碼示例
- Java AbstractSequentialList set()用法及代碼示例
- Java AbstractSequentialList isEmpty()用法及代碼示例
- Java AbstractSequentialList equals()用法及代碼示例
- Java AbstractSequentialList clear()用法及代碼示例
- Java AbstractSequentialList indexOf()用法及代碼示例
- Java AbstractSequentialList lastIndexOf()用法及代碼示例
- Java AbstractSequentialList retainAll()用法及代碼示例
- Java AbstractSequentialList toArray()用法及代碼示例
- Java AbstractSequentialList size()用法及代碼示例
- Java AbstractSequentialList toString()用法及代碼示例
- Java AbstractSequentialList conatinsAll()用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 AbstractSequentialList toArray(T[]) method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。