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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。