Home » Java programming language

Java PriorityQueue toArray() Method with Example

PriorityQueue Class toArray() method: Here, we are going to learn about the toArray() method of PriorityQueue Class with its syntax and example.
Submitted by Preeti Jain, on March 11, 2020

PriorityQueue Class toArray() method

Syntax:

    public Object[] toArray();
    public Type[] toArray(Type[] ty);
  • toArray() method is available in java.util package.
  • toArray() method is used to return an object array (Object []) that holds all of the objects in this PriorityQueue.
  • toArray(Type[] ty) method is used to return the same array as the parameter type that holds all of the objects in this PriorityQueue.
  • These methods may throw an exception at the time of returning an array.
    NullPointerException: This exception may throw when the given parameter is null exists.
  • These are non-static methods and it is accessible with the class object and if we try to access these methods with the class name then also we will get an error.

Parameter(s):

  • In the first case, toArray(), It does not accept any parameter.
  • In the first case, toArray(Type[] ty), Type[] ty – represents the array into which the sorted objects of the queue.

Return value:

In the first cases, the return type of the method is Object [], – it returns an object array with full of elements in this PriorityQueue.

In the second cases, the return type of the method is Type [], – it returns the same array as the parameter type array.

Example:

// Java program to demonstrate the example 
// of toArray() method of PriorityQueue

import java.util.*;

public class ToArrayOfPriorityQueue {
    public static void main(String args[]) {
        // Instantiate PriorityQueue
        PriorityQueue < String > pq = new PriorityQueue < String > (10);
        String[] s_arr1 = new String[5];

        // By using add() method is add
        // the given element into priority
        // queue
        pq.add("C");
        pq.add("C++");
        pq.add("JAVA");
        pq.add("PHP");
        pq.add("ANDROID");

        // Display PriorityQueue
        System.out.println("PriorityQueue: " + pq);

        // By using toArray() method is to
        // convert the given object to
        // an object array
        Object[] o_arr = pq.toArray();

        System.out.println("pq.toArray(): ");
        for (int i = 0; i < o_arr.length; ++i)
            System.out.println(o_arr[i].toString());

        System.out.println();

        // By using toArray(arr) method is to
        // contains all of the priority queue
        // elements

        String[] s_arr2 = pq.toArray(s_arr1);
        System.out.println("pq.toArray(s_arr1): ");

        for (int i = 0; i < s_arr2.length; ++i)
            System.out.println(s_arr2[i]);
    }
}

Output

PriorityQueue: [ANDROID, C, JAVA, PHP, C++]
pq.toArray(): 
ANDROID
C
JAVA
PHP
C++

pq.toArray(s_arr1): 
ANDROID
C
JAVA
PHP
C++






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.