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


Java ConcurrentLinkedQueue toArray()用法及代碼示例


  1. toArray():ConcurrentLinkedQueue的toArray()方法用於按正確順序返回與ConcurrentLinkedQueue相同的元素的數組。本質上,它將所有元素從ConcurrentLinkedQueue複製到新數組。此方法充當數組和ConcurrentLinkedQueue之間的橋梁。

    用法:

    public Object[] toArray()

    返回值:該方法返回一個數組,其中包含與ConcurrentLinkedQueue相似的元素。

    下麵的程序演示了java.util.concurrent.ConcurrentLinkedQueue.toArray()方法。


    示例1:

    // Java Program Demonstrate toArray() 
    // method of ConcurrentLinkedQueue 
      
    import java.util.concurrent.ConcurrentLinkedQueue; 
      
    public class GFG { 
      
        public static void main(String[] args) 
        { 
            // create object of ConcurrentLinkedQueue 
            ConcurrentLinkedQueue<Integer> queue 
                = new ConcurrentLinkedQueue<Integer>(); 
      
            // Add element to ConcurrentLinkedQueue 
            queue.add(2300); 
            queue.add(1322); 
            queue.add(8945); 
            queue.add(6512); 
      
            // print queue details 
            System.out.println("Queue Contains " + queue); 
      
            // apply toArray() method on queue 
            Object[] array = queue.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 ConcurrentLinkedQueue 
      
    import java.util.concurrent.ConcurrentLinkedQueue; 
      
    public class GFG { 
        public static void main(String args[]) 
        { 
            // Creating a ConcurrentLinkedQueue 
            ConcurrentLinkedQueue<String> 
                queue = new ConcurrentLinkedQueue<String>(); 
      
            // elements into the Queue 
            queue.add("Welcome"); 
            queue.add("To"); 
            queue.add("Jungle"); 
      
            // Displaying the ConcurrentLinkedQueue 
            System.out.println("The ConcurrentLinkedQueue: "
                               + queue); 
      
            // Creating the array and using toArray() 
            Object[] arr = queue.toArray(); 
      
            System.out.println("The array is:"); 
            for (int j = 0; j < arr.length; j++) 
                System.out.println(arr[j]); 
        } 
    }
    輸出:
    The ConcurrentLinkedQueue: [Welcome, To, Jungle]
    The array is:
    Welcome
    To
    Jungle
    
  2. toArray(T [] a):ConcurrentLinkedQueue的toArray(T [] a)方法用於一個數組,該數組包含與此ConcurrentLinkedQueue相同的元素,並且順序正確。此方法僅在一種情況下與toArray()不同。如果ConcurrentLinkedQueue大小小於或等於傳遞的數組,則返回的數組的類型與參數中傳遞的數組的類型相同。否則,將分配一個與指定數組相同類型的新數組,並且該數組的大小等於此隊列的大小。此方法充當數組和集合之間的橋梁。

    用法:

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

    參數:此方法將數組作為參數,如果隊列足夠大,則將隊列的所有元素都複製到其中。否則,將為它分配一個具有相同運行時類型的新數組。

    返回值:此方法返回包含與ConcurrentLinkedQueue相似的元素的數組。

    異常:此方法引發以下異常:

    • ArrayStoreException:當傳遞的數組與ConcurrentLinkedQueue的元素類型不同時。
    • NullPointerException :如果傳遞的數組為Null。

    以下示例程序旨在說明java.util.concurrent.ConcurrentLinkedQueue.toArray(T [] a)方法。

    示例1:


    // Java Program Demonstrate toArray() 
    // method of ConcurrentLinkedQueue 
      
    import java.util.concurrent.ConcurrentLinkedQueue; 
      
    public class GFG { 
      
        public static void main(String[] args) 
        { 
            // create object of ConcurrentLinkedQueue 
            ConcurrentLinkedQueue<String> queue 
                = new ConcurrentLinkedQueue<String>(); 
      
            // elements into the Queue 
            queue.add("Welcome"); 
            queue.add("To"); 
            queue.add("Jungle"); 
      
            // print queue details 
            System.out.println("Queue Contains " + queue); 
      
            // the array to pass in toArray() 
            // array has size equal to size of ConcurrentLinkedQueue 
            String[] passArray = new String[queue.size()]; 
      
            // Calling toArray(T[] a) method 
            Object[] array = queue.toArray(passArray); 
      
            // Print elements of passed array 
            System.out.println("\nThe array passed :"); 
            for (String i : passArray) { 
                System.out.print(i + " "); 
            } 
      
            System.out.println(); 
      
            // Print elements of returned array 
            System.out.println("\nThe array returned :"); 
            for (Object i : array) { 
                System.out.print(i + " "); 
            } 
        } 
    }
    輸出:
    Queue Contains [Welcome, To, Jungle]
    
    The array passed :
    Welcome To Jungle 
    
    The array returned :
    Welcome To Jungle
    

    示例2:顯示ArrayStoreException

    // Java Program Demonstrate toArray() 
    // method of ConcurrentLinkedQueue 
      
    import java.util.concurrent.ConcurrentLinkedQueue; 
      
    public class GFG { 
      
        public static void main(String[] args) 
        { 
            // create object of ConcurrentLinkedQueue 
            ConcurrentLinkedQueue<Integer> queue 
                = new ConcurrentLinkedQueue<Integer>(); 
      
            // Add element to ConcurrentLinkedQueue 
            queue.add(2323); 
            queue.add(2472); 
            queue.add(4235); 
            queue.add(1242); 
      
            // the array to pass in toArray() 
            // array has size equal to size of ConcurrentLinkedQueue 
            String[] passArray = new String[queue.size()]; 
      
            // Calling toArray(T[] a) method 
            try { 
                Object[] array = queue.toArray(passArray); 
            } 
            catch (ArrayStoreException e) { 
                System.out.println("Exception: " + e); 
            } 
        } 
    }
    輸出:
    Exception: java.lang.ArrayStoreException: java.lang.Integer
    

    示例2:顯示NullPointerException

    // Java Program Demonstrate toArray() 
    // method of ConcurrentLinkedQueue 
      
    import java.util.concurrent.ConcurrentLinkedQueue; 
      
    public class GFG { 
      
        public static void main(String[] args) 
        { 
            // create object of ConcurrentLinkedQueue 
            ConcurrentLinkedQueue<Integer> queue 
                = new ConcurrentLinkedQueue<Integer>(); 
      
            // Add element to ConcurrentLinkedQueue 
            queue.add(2323); 
            queue.add(2472); 
            queue.add(4235); 
            queue.add(1242); 
      
            // the array to pass 
            String[] passArray = null; 
      
            // Calling toArray(T[] a) method 
            try { 
                Object[] array = queue.toArray(passArray); 
            } 
            catch (NullPointerException e) { 
                System.out.println("Exception: " + e); 
            } 
        } 
    }
    輸出:
    Exception: java.lang.NullPointerException
    


相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ConcurrentLinkedQueue toArray() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。