Skip to content
Related Articles

Related Articles

LinkedTransferQueue toArray() method in Java with Examples
  • Last Updated : 08 Mar, 2019

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));
    }
}
Output:
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));
    }
}
Output:
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);
        }
    }
}
Output:
Linked Transfer Queue: [Welcome, to, Geeks, for, Geeks]
Trying to get array representation by passing null.
java.lang.NullPointerException

References:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :