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

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

Java中的 AbstractSequentialList toArray(T[]) 方法示例

Java中AbstractSequentialList类toArray(arr[])方法方法用于组成与AbstractSequentialList相同元素的数组。它以正确的顺序返回一个包含此 AbstractSequentialList 中所有元素的数组;返回数组的运行时类型是指定数组的类型。如果 AbstractSequentialList 适合指定的数组,则在其中返回。否则,使用指定数组的运行时类型和此 AbstractSequentialList 的大小分配一个新数组。
如果AbstractSequentialList 适合指定的数组并有剩余空间(即,数组的元素多于AbstractSequentialList),则数组中紧跟AbstractSequentialList 末尾的元素设置为null。 (仅当调用者知道 AbstractSequentialList 不包含任何空元素时,这对确定 AbstractSequentialList 的长度很有用。)

句法:

public  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
            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()
        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
            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()
        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
            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()
        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
            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);
  
        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