Java PriorityQueue toArray() MethodThe toArray() method of PriorityQueue class returns an array containing all the elements present in the queue where the elements are present in no particular order. The array is returned, if the queue fits in the specified array. Else, a new array is allocated with the same type of the array and size of the queue. SyntaxParameterNA a - this is the array in which the elements of the queue will be stored. Type ParametersT - this is the runtime type of the array to hold the collection. Specified ByThe toArray() method of PriorityQueue class is specified by toArray() method in interface Collection<E>. OverrideThe toArray() method of PriorityQueue class isoverrided by toArray() method in class AbstractCollection<E>. Return ValueThe toArray() method returns an array that contains all the elements of this queue. Example 1Test it NowOutput: Element : Rahul Element : Ramesh Element : Rita Element :Reema Example 2Test it NowOutput: Error:(20, 24) java: bad operand types for binary operator '>' first type: java.lang.Object second type: java.lang.Object Note: Operator ?>? cannot be applied to ?java.lang.object?.Example 3 is the solution of the above described problem. Example 3Test it NowOutput: Elements : 2 5 17 80 8 Ascending order : 2 5 8 17 80 Example 4Test it NowOutput: Exception in thread "main" java.lang.NullPointerException atjava.util.PriorityQueue.toArray(PriorityQueue.java:469) at com.javaTpoint.PriorityQueueToArrayExample5.main(PriorityQueueToArrayExample4.java:12)
Next TopicJava PriorityQueue
|