Java中的 ArrayBlockingQueue toArray() 方法
- ArrayBlockingQueue类的 toArray() 方法:
ArrayBlockingQueue 类的 toArray() 方法用于创建一个数组,其中包含与此 ArrayBlockingQueue 的元素相同的元素,并按正确的顺序排列。基本上,它将所有元素从 ArrayBlockingQueue 复制到一个新数组。此方法充当数组和集合之间的桥梁。句法:
public Object[] toArray()
返回值:该方法返回一个包含此队列中所有元素的数组。
下面的程序说明了 ArrayBlockingQueue 类的 toArray() 方法:
方案一:// Program Demonstrate how to apply toArray() method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 5; // Create object of ArrayBlockingQueue ArrayBlockingQueue
queue = new ArrayBlockingQueue (capacity); // Add 5 elements to ArrayBlockingQueue queue.offer(423); queue.offer(422); queue.offer(421); queue.offer(420); queue.offer(424); // Create a array by calling toArray() method Object[] array = queue.toArray(); // Print queue System.out.println("Queue is " + queue); // Print elements of array System.out.println("The array created by toArray() is:"); for (Object i : array) { System.out.println(i); } } } 输出:Queue is [423, 422, 421, 420, 424] The array created by toArray() is: 423 422 421 420 424
方案二:
// Program Demonstrate how to apply toArray() method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 5; // Create object of ArrayBlockingQueue ArrayBlockingQueue
queue = new ArrayBlockingQueue (capacity); // Add 5 elements to ArrayBlockingQueue queue.offer("User"); queue.offer("Employee"); queue.offer("Manager"); queue.offer("Analyst"); queue.offer("HR"); // Create a array by calling toArray() method Object[] array = queue.toArray(); // Print queue System.out.println("Queue is " + queue); // Print elements of array System.out.println("The array created by toArray() is:"); for (Object i : array) { System.out.println(i); } } } 输出:Queue is [User, Employee, Manager, Analyst, HR] The array created by toArray() is: User Employee Manager Analyst HR
- ArrayBlockingQueue类的 toArray( T[] arr ) 方法:
ArrayBlockingQueue 类的 toArray( T[] a ) 方法用于创建一个数组,该数组包含与此 ArrayBlockingQueue 的元素相同的元素,并按正确的顺序排列。它有一个附加条件。如果队列大小小于或等于指定数组,则返回数组的类型与参数中指定的数组相同。否则,分配一个与指定数组类型相同的新数组,数组的大小等于该队列的大小。此方法充当数组和集合之间的桥梁。
假设一个队列是一个 ArrayBlockingQueue 并且它只包含字符串。然后String[] new_arr = queue.toArray(new String[0]);
注意toArray(new Object[0])与 toArray() 方法相同。
句法:
public T[] toArray(T[] a)
参数:该方法采用一个参数arr ,它是一个数组,队列的所有元素都将复制到该数组中,如果它足够大的话;否则,将为其分配一个相同运行时类型的新数组。
返回值:该方法返回一个包含此队列中所有元素的数组。
异常:该方法可以抛出以下异常之一:
- ArrayStoreException :当传递的数组是不同的类型并且无法与队列的元素进行比较时。
- NullPointerException : 如果数组为 Null。
下面的程序说明了 ArrayBlockingQueue 类的 toArray(T[] a) 方法:
方案一:// Program Demonstrate how to apply toArray(T[] a) method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 5; // Create object of ArrayBlockingQueue ArrayBlockingQueue
queue = new ArrayBlockingQueue (capacity); // Add 5 elements to ArrayBlockingQueue queue.offer("User"); queue.offer("Employee"); queue.offer("Manager"); queue.offer("Analyst"); queue.offer("HR"); // Creating the array String[] array = new String[5]; // Calling toArray(T[] a) method Object[] ReturnArray = queue.toArray(array); // Print queue System.out.println("Queue is " + queue); // 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 [User, Employee, Manager, Analyst, HR] The array passed to toArray() is: User Employee Manager Analyst HR The array retuned by toArray() is: User Employee Manager Analyst HR
参考:
- https://docs.oracle.com/javase/8/docs/api/java Java
- https://docs.oracle.com/javase/8/docs/api/java Java