toArray()
PriorityBlockingQueue的toArray方法用于以适当的顺序创建一个数组,该数组具有与此PriorityBlockingQueue相同的元素。实际上,此方法将所有元素从PriorityBlockingQueue复制到新数组。此方法充当数组和PriorityBlockingQueue之间的桥梁。
用法:
public Object[] toArray()
返回值:此方法返回一个包含PriorityBlockingQueue元素的数组。
下面的程序说明PriorityBlockingQueue类的toArray()方法:
示例1:程序演示在PriorityBlockingQueue上使用toArray()方法从包含数字的PriorityBlockingQueue创建数字数组。
// 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<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(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 + " ");
}
}
}
Array Contains: 5278367 35658786 74381793 87625142
示例2:程序演示在PriorityBlockingQueue上使用toArray()方法从包含字符串值的PriorityBlockingQueue创建字符串数组。
// 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<String> names
= new PriorityBlockingQueue<String>(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> T[] toArray(T[] a)
参数:此方法将数组作为参数,如果队列足够大,则要将队列的所有元素都复制到该数组中。否则,将为它分配一个具有相同运行时类型的新数组。
返回值:此方法返回一个包含此队列中所有元素的数组。
异常此方法引发以下异常:
- ArrayStoreException:当传递的数组与PriorityBlockingQueue的元素类型不同时。
- NullPointerException :如果传递的数组为Null。
下面的程序说明PriorityBlockingQueue类的toArray(T [] a)方法:
示例1:该程序演示PriorityBlockingQueue上的toArray(T [] a)方法。
// 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<String> PrioQueue
= new PriorityBlockingQueue<String>(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 retuned by method toArray()
System.out.println();
System.out.println("The array retuned 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 retuned by toArray() is: Joyi Rani Riya
示例2:演示由PriorityBlockingQueue的toArray(T [] a)方法引发的异常的程序。
// 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<String> PrioQueue
= new PriorityBlockingQueue<String>(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/util/concurrent/PriorityBlockingQueue.html#toArray–
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#toArray-T:A-
相关用法
- Java PriorityBlockingQueue take()用法及代码示例
- Java PriorityBlockingQueue put()用法及代码示例
- Java PriorityBlockingQueue contains()用法及代码示例
- Java PriorityBlockingQueue add()用法及代码示例
- Java PriorityBlockingQueue peek()用法及代码示例
- Java PriorityBlockingQueue iterator()用法及代码示例
- Java PriorityBlockingQueue drainTo()用法及代码示例
- Java PriorityBlockingQueue clear()用法及代码示例
- Java PriorityBlockingQueue remove()用法及代码示例
- Java PriorityBlockingQueue remainingCapacity()用法及代码示例
- Java PriorityBlockingQueue spliterator()用法及代码示例
- Java PriorityBlockingQueue size()用法及代码示例
- Java PriorityBlockingQueue offer()用法及代码示例
- Java PriorityBlockingQueue poll()用法及代码示例
- Java PriorityBlockingQueue comparator()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 PriorityBlockingQueue toArray() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。