📜  Java中的 LinkedBlockingQueue toArray() 方法

📅  最后修改于: 2022-05-13 01:54:25.445000             🧑  作者: Mango

Java中的 LinkedBlockingQueue toArray() 方法

toArray()

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

句法:

public Object[] toArray()

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

下面的程序说明了 LinkedBlockingQueue 类的 toArray() 方法:

方案一:

Java
// 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 linkedQueue
            = new LinkedBlockingQueue(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");
        }
    }
}


Java
// 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 linkedQueue
            = new LinkedBlockingQueue(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 + " ");
        }
    }
}


Java
// 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 linkedQueue
            = new LinkedBlockingQueue(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 returned :");
        for (Object i : array) {
            System.out.print(i + "\t");
        }
    }
}


Java
// 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
            linkedQueue = new LinkedBlockingQueue(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);
        }
    }
}


Java
// 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
            linkedQueue = new LinkedBlockingQueue(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);
        }
    }
}


输出:
Queue Contains [2300, 1322, 8945, 6512]
The array contains:
2300    1322    8945    6512

方案二:

Java

// 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 linkedQueue
            = new LinkedBlockingQueue(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[] toArray(T[] a)

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

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

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

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

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

程序 1

Java

// 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 linkedQueue
            = new LinkedBlockingQueue(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 returned :");
        for (Object i : array) {
            System.out.print(i + "\t");
        }
    }
}
输出
Queue Contains : [Sonali, Sonam, Kajal, Komal]

The array passed :
Sonali    Sonam    Kajal    Komal    

The array returned :
Sonali    Sonam    Kajal    Komal    

程序 2:在 toArray() 中传递来自 LinkedBlockingQueue 包含的元素类型的不同类型的数组。因此 toArray() 方法将抛出异常 ArrayStoreException。

Java

// 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
            linkedQueue = new LinkedBlockingQueue(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

// 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
            linkedQueue = new LinkedBlockingQueue(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

参考:

  • https://docs.oracle.com/javase/8/docs/api/java Java
  • https://docs.oracle.com/javase/8/docs/api/java Java