El método toArray() de DelayQueue se usa para devolver una array que contiene todos los elementos en DelayQueue. Hay elementos que no están en ningún orden específico en la array.
Sintaxis:
public Object[] toArray () or public T[] toArray (T[] a)
Parámetros: este método no acepta parámetros o toma una array T[] a como parámetro, que es la array en la que se almacenarán los elementos de la lista, si es lo suficientemente grande; de lo contrario, se asigna una nueva array del mismo tipo de tiempo de ejecución para este propósito.
Valor devuelto: la función devuelve una array que contiene todos los elementos de esta lista.
Excepción: la primera sobrecarga de este método no arroja excepciones. Sin embargo, la segunda sobrecarga arroja las siguientes excepciones:
- ArrayStoreException: si el tipo de tiempo de ejecución de la array especificada no es un supertipo del tipo de tiempo de ejecución de cada elemento de esta cola.
- NullPointerException: si la array especificada es nula.
Los siguientes programas ilustran el método DelayQueue.toArray():
Programa 1:
Java
// 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; // Constructor 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}]
Programa 2:
Java
// 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; // Constructor 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}]
Publicación traducida automáticamente
Artículo escrito por ProgrammerAnvesh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA