Java中的ConcurrentLinkedQueue toArray()方法

  1. toArray(): 这个 toArray() 方法 ConcurrentLinkedQueue 用于以正确的顺序返回与ConcurrentLinkedQueue相同元素的数组。基本上,它将所有元素从ConcurrentLinkedQueue复制到一个新数组。此方法充当数组和ConcurrentLinkedQueue之间的桥梁。

    语法:

    public Object[] toArray()

    返回: 该方法返回 阵列 包含与ConcurrentLinkedQueue类似的元素。

    下面的程序演示了java。util。同时发生的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 + " " );
    }
    }
    }

    
    

    输出:

    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): 这个 toArray(T[]a) 方法 ConcurrentLinkedQueue 用于一个数组,该数组按正确的顺序包含与此ConcurrentLinkedQueue相同的元素。此方法与toArray()仅在一个条件上不同。如果ConcurrentLinkedQueue大小小于或等于传递的数组,则返回数组的类型与参数中传递的数组相同。否则,将使用与指定数组相同的类型分配一个新数组,并且该数组的大小等于此队列的大小。此方法充当数组和集合之间的桥梁。

    语法:

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

    参数: 这种方法需要 大堆 作为参数,队列的所有元素都将被复制到其中(如果队列足够大)。否则,将为该对象分配一个相同运行时类型的新数组。

    返回: 此方法返回 大堆 包含与ConcurrentLinkedQueue类似的元素。

    例外情况: 此方法引发以下异常:

    • ArrayStoreException :当传递的数组与ConcurrentLinkedQueue的元素类型不同时。
    • 空指针异常 :如果传递的数组为空。

    下面的程序演示了java。util。同时发生的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( "The array passed :" );
    for (String i : passArray) {
    System.out.print(i + " " );
    }
    System.out.println();
    // Print elements of returned array
    System.out.println( "The 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: 展示 空指针异常

    // 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
    

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享