📌  相关文章
📜  Java中的 AbstractSequentialList toArray() 方法及示例

📅  最后修改于: 2022-05-13 01:54:56.164000             🧑  作者: Mango

Java中的 AbstractSequentialList toArray() 方法及示例

Java AbstractSequentialListtoArray()方法用于形成与 AbstractSequentialList 相同元素的数组。基本上,它将 AbstractSequentialList 中的所有元素复制到一个新数组中。

句法:

Object[] arr = AbstractSequentialList.toArray()

参数:该方法不带任何参数。

返回值:该方法返回一个包含与 AbstractSequentialList 类似的元素的数组。

下面的程序说明了 AbstractSequentialList.toArray() 方法:

方案一:

// Java code to illustrate toArray()
  
import java.util.*;
  
public class AbstractSequentialListDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractSequentialList
        AbstractSequentialList
            abs_col = new LinkedList();
  
        // 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

方案二:

// Java code to illustrate toArray()
  
import java.util.*;
  
public class AbstractSequentialListDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractSequentialList
        AbstractSequentialList
            abs_col = new LinkedList();
  
        // 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