当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AbstractSequentialList toArray()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 AbstractSequentialList toArray() method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。