Vector 相似 method 釋疑
※ 引述《slalala (ptt不是丁丁知識+)》之銘言:
: 最近我也碰到這個碗糕了( ‵□′)───C<─___-)|||
: 提供一個心得。
: 我對AWT不懂,只是爬文得知這個問題。
: 因為我要弄得程式必須還要即時的JTABLE更新,
: 我發現把rapaint放在跟 JProgressBar同一個Thread裡面
: JProgressBar會變成只跑第1次,然後繼續delay到最後跑100%...
: 簡單說要是這樣寫,可能就要多一個Thread囉...
: 不過我用DefaultTableModel addROW()就解決這個問題了~_~
: 另外問一個vector的小問題
: 我英文太差嗎XD
: API我看不出Vector.add(OBJECT)
: 跟Vector.addElement(OBJECT)的差別;
這2個method的結果是一樣的
addElement() :
Adds the specified component to the end of this vector
將物件加到Vector的尾巴
This method is identical in functionality to the add(Object) method (which is
part of the List interface)
此method與 add()的功能是一樣的
真要說差別的話:
1. add() method有作overload, 有 add(index,obkect) 與 add(object) 2個,概念上是
以指定位置的方式insert物件,而add(object)只是省略指定位置,直接insert在最後
2. Vector是繼承AbstractList與實作List interface, 所以必須實作add() method
也就是add()是原本List結構所規範要有的method
3. 承2, add(Object)這個method是Collecction interface所定義的,這個method會回傳
boolean, 如果true的話,表示此Collection結構發生改變:
"Ensures that this collection contains the specified element (optional
operation). Returns true if this collection changed as a result of the call.
(Returns false if this collection does not permit duplicates and already
contains the specified element.)" (參Collection interface)
至於addElement() 並沒有回傳值
但是就Vector的使用上,並沒有差別,因為Vector並不會限制size多大,每一次的
add()都會增加size
"Collections that support this operation may place limitations on what
elements may be added to this collection. In particular, some collections
will refuse to add null elements, and others will impose restrictions on the
type of elements that may be added."
可以參考一下Collection add() API 其實蠻有意思的,包括加入重覆物件行為
的規範。其實可以提供我們在開interface的借鏡
: 還有 ElementAt(int)跟get(int)的差別
功能上也是一樣, get()是List interface規範的
最後,就使用上差別來說,用那個method會取決於你宣告的class type(靜態型別)是那種
如果宣告是:
List list = new Vector();
list.add(o);
list.get(0);
那麼就無法使用 addElement()或elementAt()了,因為class type是list,
只能使用
list.add() or list.get()
如果宣告是:
Vector vec = new Vector();
那麼2種method都能用
不過建議使用List宣告,因為實際型別可以更換而不會影響後面程式的存取,例如
如果某天你要改用LinkedList或ArrayList 後面程式不會有更動
List list = new ArrayList();
list.add(o);
list.get(0);
: 耶
: ....clear() 跟removeAllElements()
: 我好像還看的出後者是將空間釋放....
: 有人可以提示我一下嗎?
: 感謝
這2個method解釋同上囉~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.81.16.30
推
09/27 15:38, , 1F
09/27 15:38, 1F
推
09/28 12:06, , 2F
09/28 12:06, 2F