當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。