|
-
January 10th, 2012, 03:58 AM
#1
Help fix this code
when I try to it is being executed but unable to get my desired output
Desired Output:
when i call my "remove()" method
I want my Priority Queue to first give out the words starting with 's' then 'z' and then the rest.
import java.util.*;
public class PriorityCustom {
public static void main(String[] args) throws Exception {
//String[] words = {"apple", "crowd", "snow", "place", "snap", "zoo", "zing", "smile", "motto", "simple", "elegant","zebra"};
PriorityQueue<String> stringQueue = new PriorityQueue<String>(20,new Comparator<String>() {
String Z = "z";
String S = "s";
//@Override
public int compare(String S1,String S2) {
if (S1.startsWith(S)) {
return -1;
}
else if (S1.startsWith(Z)) {
return 0;
}
else {
return +1;
}
}
});
stringQueue.add("apple");
stringQueue.add("simple");
stringQueue.add("zoo");
stringQueue.add("zing");
stringQueue.add("elegant");
stringQueue.add("snap");
stringQueue.add("smile");
stringQueue.add("motto");
stringQueue.add("zebra");
stringQueue.add("crowd");
stringQueue.add("snow");
stringQueue.add("place");
/*for (int i = 0; i <0; i++)
stringQueue.add(new String());*/
while(!stringQueue.isEmpty())
System.out.println(stringQueue.remove());
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|