Java AbstractSequentialList的toArray()方法用於生成與AbstractSequentialList相同元素的數組。本質上,它將所有元素從AbstractSequentialList複製到新數組。
用法:
Object[] arr = AbstractSequentialList.toArray()
參數:該方法不帶任何參數。
返回值:該方法返回一個包含與AbstractSequentialList相似的元素的數組。
以下示例程序旨在說明AbstractSequentialList.toArray()方法:
示例1:
// Java code to illustrate toArray()
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()
Object[] arr = abs_col.toArray();
System.out.println("The array is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
輸出:
The AbstractSequentialList: [Welcome, To, Geeks, For, Geeks] The array is: Welcome To Geeks For Geeks
示例2:
// Java code to illustrate toArray()
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
// Creating an empty AbstractSequentialList
AbstractSequentialList<Integer>
abs_col = new LinkedList<Integer>();
// Use add() method to add
// elements into the AbstractSequentialList
abs_col.add(10);
abs_col.add(15);
abs_col.add(30);
abs_col.add(20);
abs_col.add(5);
abs_col.add(25);
// Displaying the AbstractSequentialList
System.out.println("The AbstractSequentialList: "
+ abs_col);
// Creating the array and using toArray()
Object[] arr = abs_col.toArray();
System.out.println("The array is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
輸出:
The AbstractSequentialList: [10, 15, 30, 20, 5, 25] The array is: 10 15 30 20 5 25
相關用法
- Java AbstractSequentialList toArray(T[])用法及代碼示例
- Java AbstractSequentialList contains()用法及代碼示例
- Java AbstractSequentialList retainAll()用法及代碼示例
- Java AbstractSequentialList.subList()用法及代碼示例
- Java AbstractSequentialList size()用法及代碼示例
- Java AbstractSequentialList isEmpty()用法及代碼示例
- Java AbstractSequentialList equals()用法及代碼示例
- Java AbstractSequentialList set()用法及代碼示例
- Java AbstractSequentialList removeAll()用法及代碼示例
- Java AbstractSequentialList set(int, Object)用法及代碼示例
- Java AbstractSequentialList get()用法及代碼示例
- Java AbstractSequentialList lastIndexOf()用法及代碼示例
- Java AbstractSequentialList toString()用法及代碼示例
- Java AbstractSequentialList iterator()用法及代碼示例
- Java AbstractSequentialList conatinsAll()用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 AbstractSequentialList toArray() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。