DelayQueue的toArray()方法用於返回包含DelayQueue中所有元素的數組。數組中沒有任何特定順序的元素。
用法:
public Object[] toArray () or public T[] toArray (T[] a)
參數:該方法或者不接受任何參數,或者將數組T [] a作為參數,如果足夠大,該數組就是要存儲列表元素的數組。否則,將為此分配一個具有相同運行時類型的新數組。
返回值:該函數返回一個包含此列表中所有元素的數組。
異常:此方法的第一次重載不會引發任何異常。但是,第二次重載引發以下異常:
- ArrayStoreException:如果指定數組的運行時類型不是此隊列中每個元素的運行時類型的超類型。
- NullPointerException :如果指定的數組為null。
以下示例程序旨在說明DelayQueue.toArray()方法:
程序1:
// Java Program Demonstrate DelayQueue toArray() method
import java.util.concurrent.*;
import java.util.*;
// The DelayObject for DelayQueue
// It must implement Delayed and
// its getDelay() and compareTo() method
class DelayObject implements Delayed {
private String name;
private long time;
// Contructor of DelayObject
public DelayObject(String name, long delayTime)
{
this.name = name;
this.time = System.currentTimeMillis()
+ delayTime;
}
// Implementing getDelay() method of Delayed
@Override
public long getDelay(TimeUnit unit)
{
long diff = time - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}
// Implementing compareTo() method of Delayed
@Override
public int compareTo(Delayed obj)
{
if (this.time < ((DelayObject)obj).time) {
return -1;
}
if (this.time > ((DelayObject)obj).time) {
return 1;
}
return 0;
}
// Implementing toString() method of Delayed
@Override
public String toString()
{
return "\n{"
+ " " + name + ", time=" + time + "}";
}
}
// Driver Class
public class GFG {
public static void main(String[] args) throws InterruptedException
{
// create object of DelayQueue
// using DelayQueue() constructor
BlockingQueue<DelayObject> DQ
= new DelayQueue<DelayObject>();
// Add numbers to end of DelayQueue
// using add() method
DQ.add(new DelayObject("A", 1));
DQ.add(new DelayObject("B", 2));
DQ.add(new DelayObject("C", 3));
DQ.add(new DelayObject("D", 4));
// print queue
System.out.println("DelayQueue: "
+ DQ);
// Get the array of the elements
// of the ArrayList
// using toArray() method
Object[] arr = DQ.toArray();
// print the array elements
System.out.println("Elements of DelayQueue"
+ " as Array: "
+ Arrays.toString(arr));
}
}
輸出:
DelayQueue: [ { A, time=1546842694862}, { B, time=1546842694863}, { C, time=1546842694864}, { D, time=1546842694865}] Elements of DelayQueue as Array: [ { A, time=1546842694862}, { B, time=1546842694863}, { C, time=1546842694864}, { D, time=1546842694865}]
程序2:
// Java Program Demonstrate DelayQueue toArray() method
import java.util.concurrent.*;
import java.util.*;
// The DelayObject for DelayQueue
// It must implement Delayed and
// its getDelay() and compareTo() method
class DelayObject implements Delayed {
private String name;
private long time;
// Contructor of DelayObject
public DelayObject(String name, long delayTime)
{
this.name = name;
this.time = System.currentTimeMillis()
+ delayTime;
}
// Implementing getDelay() method of Delayed
@Override
public long getDelay(TimeUnit unit)
{
long diff = time - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}
// Implementing compareTo() method of Delayed
@Override
public int compareTo(Delayed obj)
{
if (this.time < ((DelayObject)obj).time) {
return -1;
}
if (this.time > ((DelayObject)obj).time) {
return 1;
}
return 0;
}
// Implementing toString() method of Delayed
@Override
public String toString()
{
return "\n{"
+ " " + name + ", time=" + time + "}";
}
}
// Driver Class
public class GFG {
public static void main(String[] args) throws InterruptedException
{
// create object of DelayQueue
// using DelayQueue() constructor
BlockingQueue<DelayObject> DQ
= new DelayQueue<DelayObject>();
// Add numbers to end of DelayQueue
// using add() method
DQ.add(new DelayObject("A", 1));
DQ.add(new DelayObject("B", 2));
DQ.add(new DelayObject("C", 3));
DQ.add(new DelayObject("D", 4));
// print queue
System.out.println("DelayQueue: "
+ DQ);
// Get the array of the elements
// of the DelayQueue
// using toArray(T[]) method
Delayed arr[] = new Delayed[DQ.size()];
arr = DQ.toArray(arr);
// print the array elements
System.out.println("Elements of ArrayList"
+ " as Array: "
+ Arrays.toString(arr));
}
}
輸出:
DelayQueue: [ { A, time=1546842699503}, { B, time=1546842699504}, { C, time=1546842699505}, { D, time=1546842699506}] Elements of ArrayList as Array: [ { A, time=1546842699503}, { B, time=1546842699504}, { C, time=1546842699505}, { D, time=1546842699506}]
相關用法
- Java DelayQueue add()用法及代碼示例
- Java DelayQueue take()用法及代碼示例
- Java DelayQueue put()用法及代碼示例
- Java DelayQueue peak()用法及代碼示例
- Java DelayQueue drainTo()用法及代碼示例
- Java DelayQueue poll()用法及代碼示例
- Java DelayQueue iterator()用法及代碼示例
- Java DelayQueue offer()用法及代碼示例
- Java DelayQueue remainingCapacity()用法及代碼示例
- Java DelayQueue size()用法及代碼示例
- Java AbstractCollection toArray()用法及代碼示例
- Java SortedSet toArray()用法及代碼示例
- Java ArrayList toArray()用法及代碼示例
- Java LinkedTransferQueue toArray()用法及代碼示例
- Java Vector toArray()用法及代碼示例
注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 DelayQueue toArray() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。