GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java PriorityQueue toArray() Method
Selected Reading
Java PriorityQueue toArray() MethodDescriptionThe java PriorityQueue toArray() method returns an array containing all of the elements in this queue in proper sequence. The order in which elements are present in PriorityQueue is maintained in Array as well. DeclarationFollowing is the declaration for java.util.PriorityQueue.toArray() method public Object[] toArray() ParametersNA Return ValueThis method returns an array containing all of the elements in this queue. ExceptionNA Java PriorityQueue toArray(T[] a) MethodDescriptionThe Java PriorityQueue toArray(a) method returns an array containing all of the elements in this queue from first to last element). It returns the runtime type of the returned array as of the specified array. In case of queue fits in the specified array, it is returned. Otherwise, a new array is allocated with the specified runtime type and the size of current queue. DeclarationFollowing is the declaration for java.util.PriorityQueue.toArray(T[] a) method public <T> T[] toArray(T[] a) Parameters
Return ValueThis method returns an array containing the elements of the queue. ExceptionArrayStoreException − if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue. NullPointerException − if the specified array is null. Getting Array of Ints from a PriorityQueue of Ints ExampleThe following example shows the usage of Java PriorityQueue toArray() method with Integers. We're creating an PriorityQueue of Integers, adding some elements, and then array is created using toArray() method. The array is iterated and elements are printed. package com.tutorialspoint;
import java.util.PriorityQueue;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<Integer> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add(25);
queue.add(30);
queue.add(20);
queue.add(18);
// get the array
Object[] array = queue.toArray();
for (Object object : array) {
System.out.println(object);
}
}
}
OutputLet us compile and run the above program, this will produce the following result − 18 20 25 30 Getting Array of Strings from a PriorityQueue of Strings ExampleThe following example shows the usage of Java PriorityQueue toArray() method with Strings. We're creating an PriorityQueue of Strings, adding some elements, and then array is created using toArray() method. The array is iterated and elements are printed. package com.tutorialspoint;
import java.util.PriorityQueue;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<String> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add("A");
queue.add("B");
queue.add("C");
queue.add("D");
// get the array
Object[] array = queue.toArray();
for (Object object : array) {
System.out.println(object);
}
}
}
OutputLet us compile and run the above program, this will produce the following result − A B C D Getting Array of Objects from a PriorityQueue of Objects ExampleThe following example shows the usage of Java PriorityQueue toArray(T[]) method with Student objects. We're creating an PriorityQueue of Student objects, adding some elements, and then array is created using toArray(T[]) method. The array is iterated and elements are printed. package com.tutorialspoint;
import java.util.PriorityQueue;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<Student> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add(new Student(1, "Julie"));
queue.add(new Student(2, "Robert"));
queue.add(new Student(3, "Adam"));
// get the array of Student objects
Student[] array = queue.toArray(new Student[queue.size()]);
// print the array elements
for (Student student : array) {
System.out.println(student);
}
}
}
class Student implements Comparable<Student> {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
@Override
public boolean equals(Object obj) {
Student s = (Student)obj;
return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
}
@Override
public int compareTo(Student student) {
return this.rollNo - student.rollNo;
}
}
OutputLet us compile and run the above program, this will produce the following result − [ 1, Julie ] [ 2, Robert ] [ 3, Adam ] java_util_priorityqueue.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |