Java LinkedBlockingQueue toArray()方法
toArray()
LinkedBlockingQueen的 toArray 方法用于创建一个数组,该数组的元素与该LinkedBlockingQueen的元素相同,且顺序适当。实际上,这个方法将LinkedBlockingQueen中的所有元素复制到一个新的数组中。这个方法在数组和LinkedBlockingQueue之间起到了桥梁作用。
语法
public Object[] toArray()
返回值: 该方法返回一个包含LinkedBlockingQueue元素的数组。
下面的程序说明了LinkedBlockingQueue类的toArray()方法。
程序1 :
// Java Program Demonstrate toArray()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 50;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer> linkedQueue
= new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add(2300);
linkedQueue.add(1322);
linkedQueue.add(8945);
linkedQueue.add(6512);
// print linkedQueue details
System.out.println("Queue Contains "
+ linkedQueue);
// apply toArray() method on linkedQueue
Object[] array = linkedQueue.toArray();
// Print elements of array
System.out.println("The array contains:");
for (Object i : array) {
System.out.print(i + "\t");
}
}
}
输出
Queue Contains [2300, 1322, 8945, 6512]
The array contains:
2300 1322 8945 6512
示例2
// Java Program Demonstrate toArray()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 5;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add 5 elements to ArrayBlockingQueue
linkedQueue.offer("User");
linkedQueue.offer("Employee");
linkedQueue.offer("Manager");
linkedQueue.offer("Analyst");
linkedQueue.offer("HR");
// apply toArray() method on linkedQueue
Object[] array = linkedQueue.toArray();
// Print elements of array
System.out.println("The array contains:");
for (Object i : array) {
System.out.print(i + " ");
}
}
}
输出
The array contains:
User Employee Manager Analyst HR
toArray(T[] a)
LinkedBlockingQueen 的toArray(T[] a)方法用于返回一个包含与此LinkedBlockingQueen相同元素的数组,并以适当的顺序排列。这个方法与toArray()的不同之处只有一个条件。如果LinkedBlockingQueue的大小小于或等于所传递的数组,返回的数组的类型与参数中传递的数组相同。否则,将分配一个新的数组,其类型与指定的数组相同,并且该数组的大小等于该队列的大小。这个方法在数组和集合之间起到了桥梁作用。
语法
public <T> T[] toArray(T[] a)
参数: 该方法接收一个 数组 作为参数,如果 数组 足够大的话,队列中的所有元素将被复制到该数组中。否则,一个新的相同运行时类型的数组将被分配到此。
返回值: 该方法返回一个包含该队列中所有元素的数组。
异常 该方法会抛出以下异常。
- ArrayStoreException: 当传递的数组的类型与LinkedBlockingQueue的元素类型不同。
- NullPointerException: 如果传递的数组是Null。
下面的程序说明了LinkedBlockingQueue类的toArray(T[] a)方法。
程序1
// Java program to demonstrate
// toArray(T[] a) method
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 10;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add 5 elements to ArrayBlockingQueue
linkedQueue.offer("Sonali");
linkedQueue.offer("Sonam");
linkedQueue.offer("Kajal");
linkedQueue.offer("Komal");
// Print queue
System.out.println("Queue Contains : " + linkedQueue);
// the array to pass in toArray()
// array has size equal to size of linkedQueue
String[] passArray = new String[linkedQueue.size()];
// Calling toArray(T[] a) method
Object[] array = linkedQueue.toArray(passArray);
// Print elements of passed array
System.out.println("\nThe array passed :");
for (Object i : passArray) {
System.out.print(i + "\t");
}
System.out.println();
// Print elements of returned array
System.out.println("\nThe array returned :");
for (Object i : array) {
System.out.print(i + "\t");
}
}
}
输出
Queue Contains : [Sonali, Sonam, Kajal, Komal]
The array passed :
Sonali Sonam Kajal Komal
The array returned :
Sonali Sonam Kajal Komal
程序2: 在toArray()中传递了与LinkedBlockingQueue包含的元素类型不同的数组类型。因此,toArray()方法将抛出ArrayStoreException异常。
// Java Program Demonstrate
// toArray()
// method Exceptions.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer>
linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// add elements to queue
linkedQueue.put(46541);
linkedQueue.put(44648);
linkedQueue.put(45654);
// create a array of Strings
String[] arr = {};
// apply toArray method and Pass array of String
// as parameter to toArray method
try {
// LinkedBlockingQueue has type Integer but we are passing
// String Type array so toArray method will throw
// Exception
String[] array = linkedQueue.toArray(arr);
// print elements of queue
System.out.println("Items in Queue are " + array);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出
Exception: java.lang.ArrayStoreException: java.lang.Integer
程序3: 在toArray()中传入空数组。因此toArray()方法会抛出NullPointerException。
// Java program to demonstrate
// toArray() method Exceptions.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String>
linkedQueue = new LinkedBlockingQueue<String>(capacityOfQueue);
// add elements to queue
linkedQueue.put("aman");
linkedQueue.put("khan");
linkedQueue.put("Prakash");
// create a array of Strings
String[] arr = null;
// apply toArray method and Pass array of String
// as parameter to toArray method
try {
/// we are passing array with null values
String[] array = linkedQueue.toArray(arr);
// print elements of queue
System.out.println("Items in Queue are " + array);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出
Exception: java.lang.NullPointerException
参考资料
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#toArray-
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#toArray-T:A-
极客教程