📜  Java中的 PriorityBlockingQueue toArray() 方法

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

Java中的 PriorityBlockingQueue toArray() 方法

toArray()

PriorityBlockingQueue 的toArray方法用于创建一个与此 PriorityBlockingQueue 具有相同元素的数组,并按正确的顺序排列。实际上,此方法将 PriorityBlockingQueue 中的所有元素复制到一个新数组中。此方法充当数组和 PriorityBlockingQueue 之间的桥梁。

句法:

public Object[] toArray()

返回值:此方法返回一个包含 PriorityBlockingQueue 元素的数组。

下面的程序说明了 PriorityBlockingQueue 类的 toArray() 方法:

示例 1:演示 PriorityBlockingQueue 上的 toArray() 方法从包含数字的 PriorityBlockingQueue 创建数字数组的程序。

Java
// Java Program Demonstrate toArray()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add numbers to PriorityBlockingQueue
        PrioQueue.offer(35658786);
        PrioQueue.offer(5278367);
        PrioQueue.offer(74381793);
        PrioQueue.offer(87625142);
 
        // create an array of elements of PriorityBlockingQueue
        Object[] array = PrioQueue.toArray();
 
        // print elements of array
        System.out.println("Array Contains:");
        for (Object i : array) {
            System.out.print(i + " ");
        }
    }
}


Java
// Java Program Demonstrate toArray()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue names
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        names.offer("Joyita");
        names.offer("Bristi");
        names.offer("Riya");
 
        // Create a array by calling toArray() method
        Object[] array = names.toArray();
 
        // Print List of names
        System.out.println("Queue is " + names);
 
        // Print elements of array
        System.out.println("The array created by toArray() is:");
        for (Object i : array) {
            System.out.println(i + " ");
        }
    }
}


Java
// Java Program Demonstrate toArray(T[] a)
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 3;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        PrioQueue.offer("Joyi");
        PrioQueue.offer("Rani");
        PrioQueue.offer("Riya");
 
        // Creating the array
        String[] array = new String[capacityOfQueue];
 
        // Calling toArray(T[] a) method
        Object[] ReturnArray = PrioQueue.toArray(array);
 
        // Print queue
        System.out.println("Queue is " + PrioQueue);
 
        // Print elements of array passed as parameter
        System.out.println();
        System.out.println("The array passed to toArray() is:");
        for (Object i : array) {
            System.out.println(i);
        }
 
        // Print elements of array returned by method toArray()
        System.out.println();
        System.out.println("The array returned by toArray() is:");
        for (Object i : ReturnArray) {
            System.out.println(i);
        }
    }
}


Java
// Java Program Demonstrate toArray(T[] a)
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 3;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        PrioQueue.offer("Joyi");
        PrioQueue.offer("Rani");
        PrioQueue.offer("Riya");
 
        // Creating the array with null value
        String[] array = null;
 
        try {
            // Calling toArray(T[] a) method
            Object[] ReturnArray = PrioQueue.toArray(array);
        }
        catch (Exception e) {
            System.out.println("Exception thrown:" + e);
        }
    }
}


输出:
Array Contains:
5278367 35658786 74381793 87625142

示例 2:演示 PriorityBlockingQueue 上的 toArray() 方法从包含字符串值的 PriorityBlockingQueue 创建字符串数组的程序。

Java

// Java Program Demonstrate toArray()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue names
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        names.offer("Joyita");
        names.offer("Bristi");
        names.offer("Riya");
 
        // Create a array by calling toArray() method
        Object[] array = names.toArray();
 
        // Print List of names
        System.out.println("Queue is " + names);
 
        // Print elements of array
        System.out.println("The array created by toArray() is:");
        for (Object i : array) {
            System.out.println(i + " ");
        }
    }
}
输出:
Queue is [Bristi, Joyita, Riya]
The array created by toArray() is:
Bristi 
Joyita 
Riya

toArray(T[] a)

PriorityBlockingQueue的 toArray(T[] a) 方法用于返回一个数组,该数组包含与此 PriorityBlockingQueue 的元素相同的元素,并按正确的顺序排列。此方法与 toArray() 仅在一个条件下不同。如果 PriorityBlockingQueue 大小小于或等于传递的数组,则返回数组的类型与参数中传递的数组相同。否则,分配一个与指定数组类型相同的新数组,数组的大小等于该队列的大小。此方法充当数组和集合之间的桥梁。

句法:

public  T[] toArray(T[] a)

参数:此方法将数组作为参数,如果队列足够大,则将队列的所有元素复制到该数组中。否则,将为其分配一个相同运行时类型的新数组。

返回值:此方法返回一个包含此队列中所有元素的数组。

异常此方法抛出以下异常:

  • ArrayStoreException:当传递的数组与 PriorityBlockingQueue 的元素类型不同时。
  • NullPointerException:如果传递的数组为 Null。

下面的程序说明了 PriorityBlockingQueue 类的 toArray(T[] a) 方法:

示例 1:演示 PriorityBlockingQueue 上的 toArray(T[] a) 方法的程序。

Java

// Java Program Demonstrate toArray(T[] a)
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 3;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        PrioQueue.offer("Joyi");
        PrioQueue.offer("Rani");
        PrioQueue.offer("Riya");
 
        // Creating the array
        String[] array = new String[capacityOfQueue];
 
        // Calling toArray(T[] a) method
        Object[] ReturnArray = PrioQueue.toArray(array);
 
        // Print queue
        System.out.println("Queue is " + PrioQueue);
 
        // Print elements of array passed as parameter
        System.out.println();
        System.out.println("The array passed to toArray() is:");
        for (Object i : array) {
            System.out.println(i);
        }
 
        // Print elements of array returned by method toArray()
        System.out.println();
        System.out.println("The array returned by toArray() is:");
        for (Object i : ReturnArray) {
            System.out.println(i);
        }
    }
}
输出
Queue is [Joyi, Rani, Riya]

The array passed to toArray() is:
Joyi
Rani
Riya

The array returned by toArray() is:
Joyi
Rani
Riya

示例 2:演示 PriorityBlockingQueue 的 toArray(T[] a) 方法抛出异常的程序。

Java

// Java Program Demonstrate toArray(T[] a)
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 3;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        PrioQueue.offer("Joyi");
        PrioQueue.offer("Rani");
        PrioQueue.offer("Riya");
 
        // Creating the array with null value
        String[] array = null;
 
        try {
            // Calling toArray(T[] a) method
            Object[] ReturnArray = PrioQueue.toArray(array);
        }
        catch (Exception e) {
            System.out.println("Exception thrown:" + e);
        }
    }
}
输出:
Exception thrown:java.lang.NullPointerException

参考:

  • https://docs.oracle.com/javase/8/docs/api/java Java
  • https://docs.oracle.com/javase/8/docs/api/java Java