Java中的DelayQueue toArray()方法及示例

Java中的DelayQueue toArray()方法及示例

DelayQueue的 toArray() 方法用于返回一个包含DelayQueue中所有元素的数组。这些元素在数组中没有特定的顺序。

语法:

public Object[] toArray ()
           或
public T[] toArray (T[] a)

参数: 此方法要么不接受参数,要么接受参数数组 T[] a,该数组是列表中要存储的元素的数组,如果它足够大。否则,将针对此目的分配相同运行时类型的新数组。

返回值: 该函数返回包含此列表中所有元素的数组。

异常: 此方法的第一个重载不会抛出任何异常。但是,第二个重载会引发以下异常:

  • ArrayStoreException: 如果指定数组的运行时类型不是此队列中每个元素的运行时类型的超类型。
  • NullPointerException: 如果指定的数组为null。

以下程序说明了DelayQueue.toArray()方法:

Program 1:

// Java程序演示DelayQueue toArray()方法

import java.util.concurrent.*;
import java.util.*;

// DelayQueue的DelayObject
// 它必须实现Delayed和
//其getDelay()和compareTo()方法
class DelayObject implements Delayed {

    private String name;
    private long time;

    // DelayObject的构造函数
    public DelayObject(String name, long delayTime)
    {
        this.name = name;
        this.time = System.currentTimeMillis()
                    + delayTime;
    }

    // 实现Delayed的getDelay()方法
    @Override
    public long getDelay(TimeUnit unit)
    {
        long diff = time - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }

    // 实现Delayed的compareTo()方法
    @Override
    public int compareTo(Delayed obj)
    {
        if (this.time < ((DelayObject)obj).time) {
            return -1;
        }
        if (this.time > ((DelayObject)obj).time) {
            return 1;
        }
        return 0;
    }

    // 实现Delayed的toString()方法
    @Override
    public String toString()
    {
        return "\n{"
            + " " + name + ", time=" + time + "}";
    }
}

// 驱动程序类
public class GFG {
    public static void main(String[] args) throws InterruptedException
    {

        // 使用DelayQueue()构造函数创建DelayQueue对象
        BlockingQueue DQ
            = new DelayQueue();

        //使用add()方法将数字添加到DelayQueue的末尾
        DQ.add(new DelayObject("A", 1));
        DQ.add(new DelayObject("B", 2));
        DQ.add(new DelayObject("C", 3));
        DQ.add(new DelayObject("D", 4));

        // 打印队列
        System.out.println("DelayQueue: "
                               + DQ);

        // 使用toArray()方法获取ArrayList的元素数组
        Object[] arr = DQ.toArray();

        // 打印数组元素
        System.out.println("Elements of DelayQueue"
                               + " as Array: "
                               + Arrays.toString(arr));
    }
}

输出结果:

DelayQueue: [
{ A, time=1546842694862}, 
{ B, time=1546842694863}, 
{ C, time=1546842694864}, 
{ D, time=1546842694865}]
DelayQueue的元素作为数组: [
{ A, time=1546842694862}, 
{ B, time=1546842694863}, 
{ C, time=1546842694864}, 
{ D, time=1546842694865}]

程序2:

// Java程序演示DelayQueue toArray()方法
 
import java.util.concurrent.*;
import java.util.*;
 
// DelayQueue的DelayObject
// 它必须实现Delayed以及它的getDelay()和compareTo()方法
class DelayObject implements Delayed {
 
    private String name;
    private long time;
 
    // DelayObject的构造函数
    public DelayObject(String name, long delayTime)
    {
        this.name = name;
        this.time = System.currentTimeMillis()
                    + delayTime;
    }
 
    // 实现Delayed的getDelay()方法
    @Override
    public long getDelay(TimeUnit unit)
    {
        long diff = time - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }
 
    // 实现Delayed的compareTo()方法
    @Override
    public int compareTo(Delayed obj)
    {
        if (this.time < ((DelayObject)obj).time) {
            return -1;
        }
        if (this.time > ((DelayObject)obj).time) {
            return 1;
        }
        return 0;
    }
 
    // 实现Delayed的toString()方法
    @Override
    public String toString()
    {
        return "\n{"
            + " " + name + ", time=" + time + "}";
    }
}
 
// 驱动程序类
public class GFG {
    public static void main(String[] args) throws InterruptedException
    {
 
        // 使用DelayQueue()构造函数创建DelayQueue的对象
        BlockingQueue<DelayObject> DQ
            = new DelayQueue<DelayObject>();
 
        // 使用add()方法向DelayQueue的末尾添加元素
        DQ.add(new DelayObject("A", 1));
        DQ.add(new DelayObject("B", 2));
        DQ.add(new DelayObject("C", 3));
        DQ.add(new DelayObject("D", 4));
 
        // 打印队列
        System.out.println("DelayQueue: "
                           + DQ);
 
        // 使用toArray(T[])方法获取Delayed对象
        // 的元素数组
        Delayed arr[] = new Delayed[DQ.size()];
        arr = DQ.toArray(arr);
 
        // 打印数组的元素
        System.out.println("ArrayList的元素作为数组: "
                           + Arrays.toString(arr));
    }
}

输出:

DelayQueue: [
{ A, time=1546842699503}, 
{ B, time=1546842699504}, 
{ C, time=1546842699505}, 
{ D, time=1546842699506}]
ArrayList的元素作为数组: [
{ A, time=1546842699503}, 
{ B, time=1546842699504}, 
{ C, time=1546842699505}, 
{ D, time=1546842699506}]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程