不要使用原始的add()和remove(),在Queue中會丟出exception,
以offer()和poll()來代替
| 方法 | 傳回值 | 說明 |
| offer(E o) | boolean | 加入物件 |
| peek() | E | 取得物件,若空傳回null |
| element() | E | 取得物件,若空傳回例外 |
| poll() | E | 取得物件,並移除該物件,若空傳回null |
| remove() | E | 取得物件,並移除該物件,若空傳回例外 |
import java.util.*;
public class Ex {
public static void main(String[] args) {
Queue q = new LinkedList();
q.offer("First");
q.offer("Second");
q.offer("Third");
Object o;
System.out.println(q.toString());
while((o = q.poll()) != null) {
String s = (String)o;
System.out.println(s);
}
System.out.println(q.toString());
}
}
0 意見 :
張貼留言