所有項目

2011年5月28日 星期六

排序的方法

第一個

import java.util.*;
public class PQ {
    public static void main(String[] args){
        PriorityQueue<String> pq= new PriorityQueue<String>();
        pq.add("carrot");
        pq.add("apple");
        pq.add("banana");
  System.out.println(pq.poll()+";"+pq.peek());
Ans:apple;banana
第二個:
import java.util.*;
public class Ex_TreeSet {
public static void main(String[] args){
HashSet hs = new HashSet();
hs.add("carrot");
hs.add("apple");
hs.add("banana");
TreeSet ts = new TreeSet(hs);
Iterator it = ts.iterator();
while(it.hasNext()){
String data = (String)it.next();
System.out.println(data + ",");
}
}
}
Ans:
apple,
banana,
carrot,

沒有留言:

張貼留言