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:
import java.util.concurrent.LinkedTransferQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedTransferQueue<String> LTQ
= new LinkedTransferQueue<String>();
LTQ.add( "GeeksforGeeks" );
LTQ.add( "GFG" );
LTQ.add( "Geeks" );
LTQ.add( "gfg" );
Object[] obj = LTQ.toArray();
System.out.println( "Linked Transfer Queue: "
+ LTQ);
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:
import java.util.concurrent.LinkedTransferQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedTransferQueue<String> LTQ
= new LinkedTransferQueue<String>();
LTQ.add( "Welcome" );
LTQ.add( "to" );
LTQ.add( "Geeks" );
LTQ.add( "for" );
LTQ.add( "Geeks" );
String[] arr = new String[ 5 ];
arr = LTQ.toArray(arr);
System.out.println( "Linked Transfer Queue: "
+ LTQ);
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.
import java.util.concurrent.LinkedTransferQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedTransferQueue<String> LTQ
= new LinkedTransferQueue<String>();
LTQ.add( "Welcome" );
LTQ.add( "to" );
LTQ.add( "Geeks" );
LTQ.add( "for" );
LTQ.add( "Geeks" );
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:
Last Updated :
08 Mar, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...