Created
November 5, 2012 02:22
-
-
Save vermiculus/4014954 to your computer and use it in GitHub Desktop.
Reverse Priority Queue (Example 4 for Alan)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Comparator; | |
import java.util.PriorityQueue; | |
// Write a program that takes in 10 integers via command-line and prints them out in reverse natural order using a PriorityQueue. (this should use a Comparator) | |
public class ReversePriorityQueue | |
{ | |
public class Reverse implements Comparator<Integer> | |
{ | |
public int compare(Integer arg0, Integer arg1) | |
{ | |
return arg1 - arg0; | |
} | |
} | |
public void reverseNaturalOrder(int[] a) | |
{ | |
Reverse r = new Reverse(); | |
PriorityQueue<Integer> q = new PriorityQueue<Integer>(11, r); | |
for(int i : a) | |
q.add(i); | |
while(!q.isEmpty()) | |
{ | |
System.out.println(q.poll()); | |
} | |
} | |
public static void main(String args[]) | |
{ | |
int[] a = new int[args.length]; | |
for(int i=0; i<args.length; i++) | |
a[i] = Integer.parseInt(args[i]); | |
ReversePriorityQueue x = new ReversePriorityQueue(); | |
x.reverseNaturalOrder(a); | |
} | |
} |
artiship
commented
Mar 9, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment