Java中的PriorityQueue toArray()方法

  1. 这个 JAVAutil。优先队列。toArray() 方法用于形成与优先级队列相同元素的数组。基本上,它将所有元素从优先级队列复制到一个新数组。

    语法:

    Object[] arr = Priority_Queue.toArray()

    参数: 该方法不采用任何参数。

    返回值: 该方法返回一个数组,其中包含与优先级队列类似的元素。

    下面的程序演示了java。util。优先队列。toArray()方法。 项目1:

    // Java code to illustrate toArray()
    import java.util.*;
    public class PriorityQueueDemo {
    public static void main(String args[])
    {
    // Creating an empty PriorityQueue
    PriorityQueue<String> queue = new PriorityQueue<String>();
    // Use add() method to add elements into the Queue
    queue.add( "Welcome" );
    queue.add( "To" );
    queue.add( "Geeks" );
    queue.add( "For" );
    queue.add( "Geeks" );
    // Displaying the PriorityQueue
    System.out.println( "The PriorityQueue: " + queue);
    // Creating the array and using toArray()
    Object[] arr = queue.toArray();
    System.out.println( "The array is:" );
    for ( int j = 0 ; j < arr.length; j++)
    System.out.println(arr[j]);
    }
    }

    
    

    输出:

    The PriorityQueue: [For, Geeks, To, Welcome, Geeks]
    The array is:
    For
    Geeks
    To
    Welcome
    Geeks
    

    项目2:

    // Java code to illustrate toArray()
    import java.util.*;
    public class PriorityQueueDemo {
    public static void main(String args[])
    {
    // Creating an empty PriorityQueue
    PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
    // Use add() method to add elements into the Queue
    queue.add( 10 );
    queue.add( 15 );
    queue.add( 30 );
    queue.add( 20 );
    queue.add( 5 );
    queue.add( 25 );
    // Displaying the PriorityQueue
    System.out.println( "The PriorityQueue: " + queue);
    // Creating the array and using toArray()
    Object[] arr = queue.toArray();
    System.out.println( "The array is:" );
    for ( int j = 0 ; j < arr.length; j++)
    System.out.println(arr[j]);
    }
    }

    
    

    输出:

    The PriorityQueue: [5, 10, 25, 20, 15, 30]
    The array is:
    5
    10
    25
    20
    15
    30
    

  2. 这个 JAVAutil。优先队列。toArray(arr[] 方法用于形成与优先级队列相同元素的数组。基本上,它将所有元素从优先级队列复制到一个新数组。它创建了多个数组,这与之前没有参数的方法不同。此方法将所有元素复制到arr[]中。 语法:
    Object[] arr1 = Priority_Queue.toArray(arr[])

    参数: 该方法接受一个参数 arr[] 队列的所有元素都要复制到其中。

    返回值: 该方法返回一个数组,其中包含与优先级队列类似的元素。

      例外情况: 该方法可能引发两种类型的异常:

    • ArrayStoreException:当提到的数组属于不同类型,并且无法与队列中提到的元素进行比较时。
    • NullPointerException:如果数组为Null,则引发此异常。

    下面的程序说明了java的工作原理。util。优先队列。toArray(arr[])方法。

    // Java code to illustrate toArray(arr[])
    import java.util.*;
    public class PriorityQueueDemo {
    public static void main(String args[])
    {
    // Creating an empty PriorityQueue
    PriorityQueue<String> queue = new PriorityQueue<String>();
    // Use add() method to add elements into the Queue
    queue.add( "Welcome" );
    queue.add( "To" );
    queue.add( "Geeks" );
    queue.add( "For" );
    queue.add( "Geeks" );
    // Displaying the PriorityQueue
    System.out.println( "The PriorityQueue: " + queue);
    // Creating the array and using toArray()
    String[] arr = new String[ 5 ];
    String[] arr1 = queue.toArray(arr);
    // Displaying arr
    System.out.println( "The arr[] is:" );
    for ( int j = 0 ; j < arr.length; j++)
    System.out.println(arr[j]);
    // Displaying arr1
    System.out.println();
    System.out.println( "The arr1[] is:" );
    for ( int i = 0 ; i < arr1.length; i++)
    System.out.println(arr1[i]);
    }
    }

    
    

    输出:

    The PriorityQueue: [For, Geeks, To, Welcome, Geeks]
    The arr[] is:
    For
    Geeks
    To
    Welcome
    Geeks
    
    The arr1[] is:
    For
    Geeks
    To
    Welcome
    Geeks
    

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享