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


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


toArray()

LinkedBlockingQueue的toArray方法用于按正确的顺序创建一个具有与此LinkedBlockingQueue相同元素的数组。实际上,此方法将所有元素从LinkedBlockingQueue复制到新数组。此方法充当数组和LinkedBlockingQueue之间的桥梁。

用法:

public Object[] toArray()

返回值:此方法返回一个包含LinkedBlockingQueue元素的数组。


以下示例程序旨在说明LinkedBlockingQueue类的toArray()方法:

示例1:

// Java Program Demonstrate toArray() 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 50; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<Integer> linkedQueue 
            = new LinkedBlockingQueue<Integer>(capacityOfQueue); 
  
        // Add element to LinkedBlockingQueue 
        linkedQueue.add(2300); 
        linkedQueue.add(1322); 
        linkedQueue.add(8945); 
        linkedQueue.add(6512); 
  
        // print linkedQueue details 
        System.out.println("Queue Contains "
                           + linkedQueue); 
  
        // apply toArray() method on linkedQueue 
        Object[] array = linkedQueue.toArray(); 
  
        // Print elements of array 
        System.out.println("The array contains:"); 
        for (Object i : array) { 
            System.out.print(i + "\t"); 
        } 
    } 
}
输出:
Queue Contains [2300, 1322, 8945, 6512]
The array contains:
2300    1322    8945    6512

示例2:

// Java Program Demonstrate toArray() 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 5; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<String> linkedQueue 
            = new LinkedBlockingQueue<String>(capacityOfQueue); 
  
        // Add 5 elements to ArrayBlockingQueue 
        linkedQueue.offer("User"); 
        linkedQueue.offer("Employee"); 
        linkedQueue.offer("Manager"); 
        linkedQueue.offer("Analyst"); 
        linkedQueue.offer("HR"); 
  
        // apply toArray() method on linkedQueue 
        Object[] array = linkedQueue.toArray(); 
  
        // Print elements of array 
        System.out.println("The array contains:"); 
        for (Object i : array) { 
            System.out.print(i + " "); 
        } 
    } 
}
输出:
The array contains:
User Employee Manager Analyst HR

toArray(T[] a)

LinkedBlockingQueue的toArray(T [] a)方法用于按正确顺序返回包含与该LinkedBlockingQueue相同的元素的数组。此方法仅在一种情况下与toArray()不同。如果LinkedBlockingQueue的大小小于或等于传递的数组,则返回的数组的类型与参数中传递的数组的类型相同。否则,将分配一个与指定数组相同类型的新数组,并且该数组的大小等于此队列的大小。此方法充当数组和集合之间的桥梁。

用法:

public <T> T[] toArray(T[] a)

参数:此方法将数组作为参数,如果队列足够大,则要将队列的所有元素都复制到该数组中。否则,将为它分配一个具有相同运行时类型的新数组。

返回值:此方法返回一个包含此队列中所有元素的数组。

异常此方法引发以下异常:


  • ArrayStoreException:当传递的数组与LinkedBlockingQueue的元素类型不同时。
  • NullPointerException :如果传递的数组为Null。

下面的程序演示了LinkedBlockingQueue类的toArray(T [] a)方法:

程序1

// Java program to demonstrate 
// toArray(T[] a) method 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 10; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<String> linkedQueue 
            = new LinkedBlockingQueue<String>(capacityOfQueue); 
  
        // Add 5 elements to ArrayBlockingQueue 
        linkedQueue.offer("Sonali"); 
        linkedQueue.offer("Sonam"); 
        linkedQueue.offer("Kajal"); 
        linkedQueue.offer("Komal"); 
  
        // Print queue 
        System.out.println("Queue Contains : " + linkedQueue); 
  
        // the array to pass in toArray() 
        // array has size equal to size of linkedQueue 
        String[] passArray = new String[linkedQueue.size()]; 
  
        // Calling toArray(T[] a) method 
        Object[] array = linkedQueue.toArray(passArray); 
  
        // Print elements of passed array 
        System.out.println("\nThe array passed :"); 
        for (Object i : passArray) { 
            System.out.print(i + "\t"); 
        } 
  
        System.out.println(); 
  
        // Print elements of returned array 
        System.out.println("\nThe array retuned :"); 
        for (Object i : array) { 
            System.out.print(i + "\t"); 
        } 
    } 
}
输出:
Queue Contains : [Sonali, Sonam, Kajal, Komal]

The array passed :
Sonali    Sonam    Kajal    Komal    

The array retuned :
Sonali    Sonam    Kajal    Komal

示例2:从LinkedBlockingQueue包含的元素类型在toArray()中传递不同类型的数组。 toArray()方法将因此引发异常ArrayStoreException。

// Java Program Demonstrate 
// toArray() 
// method Exceptions. 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws InterruptedException 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 4; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<Integer> 
            linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue); 
  
        // add elements to queue 
        linkedQueue.put(46541); 
        linkedQueue.put(44648); 
        linkedQueue.put(45654); 
  
        // create a array of Strings 
        String[] arr = {}; 
  
        // apply toArray method and Pass array of String 
        // as parameter to toArray method 
        try { 
            // LinkedBlockingQueue has type Integer but we are passing 
            // String Type array so toArray method will throw 
            // Exception 
            String[] array = linkedQueue.toArray(arr); 
  
            // print elements of queue 
            System.out.println("Items in Queue are " + array); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
输出:
Exception: java.lang.ArrayStoreException: java.lang.Integer

示例3:在toArray()中传递空数组。因此,toArray()方法将抛出NullPointerException。

// Java program to demonstrate 
// toArray() method Exceptions. 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws InterruptedException 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 4; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<String> 
            linkedQueue = new LinkedBlockingQueue<String>(capacityOfQueue); 
  
        // add elements to queue 
        linkedQueue.put("aman"); 
        linkedQueue.put("khan"); 
        linkedQueue.put("Prakash"); 
  
        // create a array of Strings 
        String[] arr = null; 
  
        // apply toArray method and Pass array of String 
        // as parameter to toArray method 
        try { 
  
            /// we are passing array with null values 
            String[] array = linkedQueue.toArray(arr); 
  
            // print elements of queue 
            System.out.println("Items in Queue are " + array); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
输出:
Exception: java.lang.NullPointerException

参考:



相关用法


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