-
toArray()方法ArrayBlockingQueue類:
ArrayBlockingQueue類的toArray()方法用於按適當順序創建一個數組,該數組包含與此ArrayBlockingQueue相同的元素。本質上,它將所有元素從ArrayBlockingQueue複製到新數組。此方法充當數組和集合之間的橋梁。用法:
public Object[] toArray()
返回值:該方法返回一個包含此隊列中所有元素的數組。
下麵的程序說明ArrayBlockingQueue類的toArray()方法:
示例1:// 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<Integer> queue = new ArrayBlockingQueue<Integer>(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
示例2:
// 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<String> queue = new ArrayBlockingQueue<String>(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
-
toArray(T [] arr) 方法ArrayBlockingQueue類:
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)方法:
示例1:// 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<String> queue = new ArrayBlockingQueue<String>(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/util/concurrent/ArrayBlockingQueue.html#toArray
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html#toArray-T:A
相關用法
- Java ArrayBlockingQueue contains()用法及代碼示例
- Java ArrayBlockingQueue add()用法及代碼示例
- Java ArrayBlockingQueue put()用法及代碼示例
- Java ArrayBlockingQueue take()用法及代碼示例
- Java ArrayBlockingQueue poll()用法及代碼示例
- Java ArrayBlockingQueue iterator()用法及代碼示例
- Java ArrayBlockingQueue size()用法及代碼示例
- Java ArrayBlockingQueue remove()用法及代碼示例
- Java ArrayBlockingQueue remainingCapacity()用法及代碼示例
- Java ArrayBlockingQueue spliterator()用法及代碼示例
- Java ArrayBlockingQueue clear()用法及代碼示例
- Java ArrayBlockingQueue offer()用法及代碼示例
- Java ArrayBlockingQueue toString()用法及代碼示例
- Java ArrayBlockingQueue drainTo()用法及代碼示例
- Java ArrayBlockingQueue peek()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayBlockingQueue toArray() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。