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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。