網頁

2014年11月27日 星期四

[Android]Queue(FIFO)

Reference:
Queue | Android Developments
http://developer.android.com/reference/java/util/Queue.html

http://jhengjyun.blogspot.tw/2010/09/java-queue.html

不要使用原始的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((= q.poll()) != null) { 
            String s = (String)o; 
            System.out.println(s); 
        } 
        System.out.println(q.toString()); 
    } 
}

沒有留言:

張貼留言