大家好,又见面了,我是你们的朋友全栈君。
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 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 + ” “);
}
}
}
输出:
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 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 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 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 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
参考:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/146609.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...