The toArray() method of ArrayBlockingQueue class: The toArray() method of ArrayBlockingQueue class is used to create an array containing the same elements as that of this ArrayBlockingQueue, in proper sequence. Basically, it copies all the element from the ArrayBlockingQueue to a new array. This method behaves as a bridge between array and collections. Syntax:
public Object[] toArray()
Return Value: The method returns an array containing all of the elements in this queue. Below programs illustrate toArray() method of ArrayBlockingQueue class: Program 1:
Java
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacity = 5 ;
ArrayBlockingQueue<Integer> queue = new
ArrayBlockingQueue<Integer>(capacity);
queue.offer( 423 );
queue.offer( 422 );
queue.offer( 421 );
queue.offer( 420 );
queue.offer( 424 );
Object[] array = queue.toArray();
System.out.println("Queue is " + queue);
System.out.println("The array created by toArray() is:");
for (Object i : array) {
System.out.println(i);
}
}
}
|
Output:
Queue is [423, 422, 421, 420, 424]
The array created by toArray() is:
423
422
421
420
424
Program 2:
Java
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacity = 5 ;
ArrayBlockingQueue<String> queue = new
ArrayBlockingQueue<String>(capacity);
queue.offer("User");
queue.offer("Employee");
queue.offer("Manager");
queue.offer("Analyst");
queue.offer("HR");
Object[] array = queue.toArray();
System.out.println("Queue is " + queue);
System.out.println("The array created by toArray() is:");
for (Object i : array) {
System.out.println(i);
}
}
}
|
Output:
Queue is [User, Employee, Manager, Analyst, HR]
The array created by toArray() is:
User
Employee
Manager
Analyst
HR
The toArray(T[] arr) method of ArrayBlockingQueue class: The toArray(T[] a) method of ArrayBlockingQueue class is used to create an array containing the same elements as that of this ArrayBlockingQueue, in proper sequence. It has an additional condition. The type of the returned array is the same as the specified array in the parameter if the queue size is less than or equal to the specified array. Otherwise, a new array is allocated with the type same as the specified array and size of the array is equal to the size of this queue. This method behaves as a bridge between array and collections. Suppose a queue is an ArrayBlockingQueue and it contains only strings. Then
String[] new_arr = queue.toArray(new String[0]);
Note that toArray(new Object[0]) is same toArray() Method. Syntax:
public T[] toArray(T[] a)
Parameters: The method takes one parameter the arr which is an array into which all of the elements of the queue are to be copied, if it is big enough; otherwise, a new array of the same runtime type is allocated to this. Return Value: The method returns an array containing all of the elements in this queue. Exception: The method can throw one of the following exceptions:
- ArrayStoreException: When the passed array is of the different type and is not able to compare with the elements of the queue.
- NullPointerException: If the array is Null.
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Jan, 2023
Like Article
Save Article