当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ArrayBlockingQueue toArray()用法及代码示例


  1. toArray()方法ArrayBlockingQueue类:
    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
    
  2. toArray(T [] arr) 方法ArrayBlockingQueue
    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 :如果数组为Null。

    下面的程序说明ArrayBlockingQueue类的toArray(T [] a)方法:
    示例1:

    // Program Demonstrate how to apply toArray(T[] a) 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"); 
      
            // Creating the array 
            String[] array = new String[5]; 
      
            // Calling toArray(T[] a) method 
            Object[] ReturnArray = queue.toArray(array); 
      
            // Print queue 
            System.out.println("Queue is " + queue); 
      
            // Print elements of array passed as parameter 
            System.out.println(); 
            System.out.println("The array passed to toArray() is:"); 
            for (Object i : array) { 
                System.out.println(i); 
            } 
      
            // Print elements of array retuned by method toArray() 
            System.out.println(); 
            System.out.println("The array retuned by toArray() is:"); 
            for (Object i : ReturnArray) { 
                System.out.println(i); 
            } 
        } 
    }
    输出:
    Queue is [User, Employee, Manager, Analyst, HR]
    
    The array passed to toArray() is:
    User
    Employee
    Manager
    Analyst
    HR
    
    The array retuned by toArray() is:
    User
    Employee
    Manager
    Analyst
    HR
    

参考:



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ArrayBlockingQueue toArray() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。