關於Java中Stack、Queue的一些api
阿新 • • 發佈:2018-12-24
關於Java中Queue的offer和add方法的區別
API中這樣說到:
add():Inserts the specified element at the tail of this queue. As the queue is unbounded, this method will never throw IllegalStateException or return false.
offer():Inserts the specified element at the tail of this queue. As the queue is unbounded, this method will never return false.
區別:兩者都是往佇列尾部插入元素,不同的時候,當超出佇列界限的時候,add()方法是丟擲異常讓你處理,而offer()方法是直接返回false
關於Java中Stack中isEmpty和empty的區別
原始碼如下:
public synchronized boolean isEmpty() {
return elementCount == 0;
}
public synchronized int size() {
return elementCount;
}
public boolean empty() {
return size() == 0;
}