LinkedBlockingQueue toArray()方法在Java中

LinkedBlockingQueue toArray()方法在Java中

toArray()

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

语法:

public Object[] toArray()

返回值: 该方法返回包含LinkedBlockingQueue元素的数组。

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

程序1:

// Java示例演示LinkedBlockingQueue的toArray()方法

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

    public static void main(String[] args)
    {
        // 定义LinkedBlockingQueue的容量
        int queueCapacity = 50;

        // 创建LinkedBlockingQueue对象
        LinkedBlockingQueue<Integer> linkedQueue
            = new LinkedBlockingQueue<Integer>(queueCapacity);

        // 向LinkedBlockingQueue添加元素
        linkedQueue.add(2300);
        linkedQueue.add(1322);
        linkedQueue.add(8945);
        linkedQueue.add(6512);

        // 打印linkedQueue的详细信息
        System.out.println("队列包含" + linkedQueue);

        // 在linkedQueue上应用toArray()方法
        Object[] array = linkedQueue.toArray();

        // 打印数组的元素
        System.out.println("数组包含:");
        for (Object i : array) {
            System.out.print(i + "\t");
        }
    }
}

输出:

队列包含[2300, 1322, 8945, 6512]
数组包含:
2300    1322    8945    6512

程序2:

// Java示例演示LinkedBlockingQueue的toArray()方法

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {
    public static void main(String[] args)
    {
        // 定义LinkedBlockingQueue的容量
        int queueCapacity = 5;

        // 创建LinkedBlockingQueue对象
        LinkedBlockingQueue<String> linkedQueue
            = new LinkedBlockingQueue<String>(queueCapacity);

        // 向LinkedBlockingQueue添加5个元素
        linkedQueue.offer("用户");
        linkedQueue.offer("员工");
        linkedQueue.offer("经理");
        linkedQueue.offer("分析师");
        linkedQueue.offer("人力资源");

        // 在linkedQueue上应用toArray()方法
        Object[] array = linkedQueue.toArray();

        // 打印数组的元素
        System.out.println("数组包含:");
        for (Object i : array) {
            System.out.print(i + " ");
        }
    }
}

输出:

数组包含:
用户 员工 经理 分析师 人力资源

toArray(T[] a)

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

语法:

public <T> T[] toArray(T[] a)
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)
    {
        // 定义 LinkedBlockingQueue 的容量
        int capacityOfQueue = 10;

        // 创建 LinkedBlockingQueue 对象
        LinkedBlockingQueue<String> linkedQueue
            = new LinkedBlockingQueue<String>(capacityOfQueue);

        // 向 ArrayBlockingQueue 添加 5 个元素
        linkedQueue.offer("Sonali");
        linkedQueue.offer("Sonam");
        linkedQueue.offer("Kajal");
        linkedQueue.offer("Komal");

        // 打印队列
        System.out.println("Queue Contains : " + linkedQueue);

        // 传递给 toArray() 方法的数组,其大小等于 linkedQueue 的大小
        String[] passArray = new String[linkedQueue.size()];

        // 调用 toArray(T[] a) 方法
        Object[] array = linkedQueue.toArray(passArray);

        // 打印传递的数组元素
        System.out.println("\nThe array passed :");
        for (Object i : passArray) {
            System.out.print(i + "\t");
        }

        System.out.println();

        // 打印返回数组元素
        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程序演示
// toArray()
// 方法的异常。
 
import java.util.concurrent.LinkedBlockingQueue;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // 定义LinkedBlockingQueue的容量
        int capacityOfQueue = 4;
 
        // 创建LinkedBlockingQueue对象
        LinkedBlockingQueue<Integer>
            linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
 
        // 向队列添加元素
        linkedQueue.put(46541);
        linkedQueue.put(44648);
        linkedQueue.put(45654);
 
        // 创建字符串数组
        String[] arr = {};
 
        // 调用toArray方法,并将字符串数组作为参数传递给toArray方法
        try {
            // LinkedBlockingQueue的类型为Integer,但是我们传递的是
            // 字符串类型数组,因此toArray方法会抛出异常
            String[] array = linkedQueue.toArray(arr);
 
            // 打印队列中的元素
            System.out.println("队列中的元素为 " + array);
        }
        catch (Exception e) {
            System.out.println("异常: " + e);
        }
    }
}

输出:

异常: java.lang.ArrayStoreException: java.lang.Integer

程序3: 将null数组传递给toArray()。因此,toArray()方法会抛出NullPointerException。

// Java程序演示
// toArray()方法的异常。
 
import java.util.concurrent.LinkedBlockingQueue;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // 定义LinkedBlockingQueue的容量
        int capacityOfQueue = 4;
 
        // 创建LinkedBlockingQueue对象
        LinkedBlockingQueue<String>
            linkedQueue = new LinkedBlockingQueue<String>(capacityOfQueue);
 
        // 向队列添加元素
        linkedQueue.put("aman");
        linkedQueue.put("khan");
        linkedQueue.put("Prakash");
 
        // 创建一个字符串数组
        String[] arr = null;
 
        // 调用toArray方法,并将字符串数组作为参数传递给toArray方法
        try {
 
            // 我们传递了空值数组
            String[] array = linkedQueue.toArray(arr);
 
            // 打印队列中的元素
            System.out.println("队列中的元素为 " + array);
        }
        catch (Exception e) {
            System.out.println("异常: " + e);
        }
    }
}

输出:

异常: java.lang.NullPointerException

参考:

  • https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#toArray–
  • https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#toArray-T:A-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程