Java ArrayBlockingQueue toArray()方法

Java ArrayBlockingQueue toArray()方法

ArrayBlockingQueue 的toArray ()方法 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

ArrayBlockingQueue 的toAr ray( T[] arr )方法 :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 : 如果数组是空的。

参考资料

  • 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程