Java AbstractCollection toArray()方法及示例

Java AbstractCollection toArray()方法及示例

1.toArray()

Java AbstractCollection的 toArray() 方法用于形成一个与AbstractCollection相同元素的数组。基本上,它把一个抽象集合中的所有元素复制到一个新的数组中。

语法

Object[] arr = AbstractCollection.toArray()

参数: 该方法不接受任何参数。

返回值: 该方法返回一个包含元素的数组,类似于AbstractCollection。

下面的程序说明了AbstractCollection.toArray()方法。

程序1 :

// Java code to illustrate toArray()
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + 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 AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The array is:
For
Geeks
To
Welcome
Geeks

程序 2:

// Java code to illustrate toArray()
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<Integer>
            abs_col = new PriorityQueue<Integer>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        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 AbstractCollection
        System.out.println("The AbstractCollection: "
                           + 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 AbstractCollection: [5, 10, 25, 20, 15, 30]
The array is:
5
10
25
20
15
30

2. toArray(arr[])

Java中AbstractCollection类的 toArray(arr[]) 方法是用来形成一个与AbstractCollection相同元素的数组。它返回一个包含AbstractCollection中所有元素的数组 ,顺序正确; 返回的数组的运行时类型是指定数组的类型。如果AbstractCollection适合于指定的数组,它将被返回。否则,一个新的数组被分配,其运行时类型是指定的数组和这个AbstractCollection的大小。
如果AbstractCollection适合在指定的数组中,并且有剩余的空间(即,数组的元素比AbstractCollection多),紧随AbstractCollection结束的数组中的元素被设置为空。(只有当调用者知道AbstractCollection不包含任何空元素时,这对确定AbstractCollection的长度才是有用的。)

语法

Object[] arr1 = AbstractCollection.toArray(arr[])

参数: 该方法接受一个参数arr[],如果该数组足够大的话,它是存储AbstractCollection元素的数组;否则,将为此目的分配一个相同运行时类型的新数组。

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

异常: 该方法可能抛出两种类型的异常。

  • ArrayStoreException : 当提到的数组是不同的类型并且不能与AbstractCollection中提到的元素进行比较。
  • NullPointerException : 如果数组是空的,就会抛出这个异常。

下面的程序说明了AbstractCollection.toArray(arr[])方法的工作。

程序1:当数组的大小与抽象集合相同时

// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + 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 AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The arr[] is:
For
Geeks
To
Welcome
Geeks

程序2:当数组小于AbstractCollection的大小时

// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + 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 AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The arr[] is:
For
Geeks
To
Welcome
Geeks

程序3:当数组大于AbstractCollection的大小时

// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + 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 AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The arr[] is:
For
Geeks
To
Welcome
Geeks
null
null
null
null
null

程序4:演示NullPointerException

// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + 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 AbstractCollection: [For, Geeks, To, Welcome, Geeks]
Exception: java.lang.NullPointerException

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程