LinkedTransferQueue toArray() method in Java with Examples
public Object[] toArray()
The toArray() method of Java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to form an array of the same elements as that of LinkedTransferQueue. Basically, it copies all the element to the new array from the LinkedTransferQueue.
Syntax:
public Object[] toArray()
Parameters: This method do not accepts any parameter.
Returns: This method returns an array containing all the elements of this queue, as its array representation.
Exceptions: This method do not throw any exception.
Below program illustrates the toArray() function of LinkedTransferQueue class:
Program 1:
// Java code to illustrate // toarray() method of LinkedTransferQueue import java.util.concurrent.LinkedTransferQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of LinkedTransferQueue LinkedTransferQueue<String> LTQ = new LinkedTransferQueue<String>(); // Add numbers to end of LinkedTransferQueue // using add() method LTQ.add("GeeksforGeeks"); LTQ.add("GFG"); LTQ.add("Geeks"); LTQ.add("gfg"); // Get the array using toArray() method Object[] obj = LTQ.toArray(); // Print the LTQ System.out.println("Linked Transfer Queue: " + LTQ); // Print the array System.out.println("Array representation: " + Arrays.toString(obj)); } } |
Linked Transfer Queue: [GeeksforGeeks, GFG, Geeks, gfg] Array representation: [GeeksforGeeks, GFG, Geeks, gfg]
public T[] toArray(T[] a)
The toArray() method of Java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to form an array of the same elements as that of the LinkedTransferQueue. It returns an array containing all of the elements in this LinkedTransferQueue in the correct order; the run-time type of the returned array is that of the specified array. If the LinkedTransferQueue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this LinkedTransferQueue.
Syntax:
public T[] toArray (T[] a)
Parameters: The method accepts one parameter a[] which is the array into which the elements of the Vector are to be stored if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns: This method returns an array containing all the elements of this queue.
Exceptions: This method throws following exceptions:
- ArrayStoreException: if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue
- NullPointerException: if the specified array is null
Program 2:
// Java code to illustrate // toarray(a[]) method of LinkedTransferQueue import java.util.concurrent.LinkedTransferQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // Create object of LinkedTransferQueue LinkedTransferQueue<String> LTQ = new LinkedTransferQueue<String>(); // Add numbers to end of LinkedTransferQueue // using add() method LTQ.add("Welcome"); LTQ.add("to"); LTQ.add("Geeks"); LTQ.add("for"); LTQ.add("Geeks"); // Get the array using toArray() method String[] arr = new String[5]; arr = LTQ.toArray(arr); // Print the LTQ System.out.println("Linked Transfer Queue: " + LTQ); // Print the array System.out.println("Array representation: " + Arrays.toString(arr)); } } |
Linked Transfer Queue: [Welcome, to, Geeks, for, Geeks] Array representation: [Welcome, to, Geeks, for, Geeks]
Program 3: Program to show null pointer exception.
// Java code to illustrate // toarray(a[]) method of LinkedTransferQueue import java.util.concurrent.LinkedTransferQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of LinkedTransferQueue LinkedTransferQueue<String> LTQ = new LinkedTransferQueue<String>(); // Add numbers to end of LinkedTransferQueue // using add() method LTQ.add("Welcome"); LTQ.add("to"); LTQ.add("Geeks"); LTQ.add("for"); LTQ.add("Geeks"); // Print the LTQ System.out.println("Linked Transfer Queue: " + LTQ); try { System.out.println("Trying to get array" + " representation by " + "passing null."); LTQ.toArray(null); } catch (Exception e) { System.out.println(e); } } } |
Linked Transfer Queue: [Welcome, to, Geeks, for, Geeks] Trying to get array representation by passing null. java.lang.NullPointerException
References:
- https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/LinkedTransferQueue.html#toArray()
- https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/LinkedTransferQueue.html#toArray(T%5B%5D)
Recommended Posts:
- LinkedTransferQueue removeAll() method in Java with Examples
- LinkedTransferQueue toString() method in Java with Examples
- LinkedTransferQueue removeIf() method in Java with Examples
- LinkedTransferQueue forEach() method in Java with Examples
- LinkedTransferQueue remainingCapacity() method in Java with Examples
- LinkedTransferQueue retainAll() method in Java with Examples
- LinkedTransferQueue getWaitingConsumerCount() method in Java with Examples
- LinkedTransferQueue tryTransfer() method in Java with Examples
- LinkedTransferQueue transfer() method in Java with Examples
- Vector toArray() Method in Java with Examples
- DelayQueue toArray() method in Java with Examples
- AbstractCollection toArray() Method in Java with Examples
- ArrayList toArray() method in Java with Examples
- Java Guava | Doubles.toArray() method with Examples
- Java Guava | Floats.toArray() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



