Java學(xué)習(xí)筆記27
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-03-27 08:18:56 閱讀次數(shù):2570次
Vector是List接口的實(shí)現(xiàn)類,支持List接口的全部功能,Vector類是基于數(shù)組實(shí)現(xiàn)的List類,在內(nèi)部封裝了1個(gè)動(dòng)態(tài)的、
允許再分配的Object[]數(shù)組,Vector是線程安全的,不必程序保證該集合的同步性。
以下是Vector類的1部份方法使用說(shuō)明:
public class Main {
public static void main(String[] args) {
Vector vector=new Vector();
ArrayList list=new ArrayList();
list.add("BILL");
vector.add(list);
//輸出:[[BILL]]
System.out.println(vector);
//輸出:vector容量:10
System.out.println("vector容量:"+vector.capacity());
/*
* 增加此向量的容量(如有必要),以確保其最少能夠保存最小容量參數(shù)指定的組件數(shù)。
* 如果此向量確當(dāng)前容量小于 minCapacity,則通過(guò)將其內(nèi)部數(shù)據(jù)數(shù)組(保存在字段 elementData 中)
* 替換為1個(gè)較大的數(shù)組來(lái)增加其容量。新數(shù)據(jù)數(shù)組的大小將為原來(lái)的大小加上 capacityIncrement,
* 除非 capacityIncrement 的值小于等于零,在后1種情況下,新的容量將為原來(lái)容量的兩倍,
* 不過(guò),如果此大小依然小于 minCapacity,則新容量將為 minCapacity。
*/
vector.ensureCapacity(21);
//輸出:vector容量:21
System.out.println("vector容量:"+vector.capacity());
Vector vector1=new Vector();
vector1.add(list);
/*
* 如果指定的 Object 與此向量相等,則返回 true
*/
System.out.println(vector.equals(vector1));//輸出:true
/*
* 返回此向量的第1個(gè)組件(位于索引 0) 處的項(xiàng))。
*/
System.out.println(vector.firstElement());//輸出:[BILL]
/*
* 返回向量中指定位置的元素。
* 如果索引超越范圍 (index < 0 || index >= size()),拋出ArrayIndexOutOfBoundsException 異常
*/
System.out.println(vector.get(0));//輸出:[BILL]
/*
* 返回此向量的哈希碼值。
*/
System.out.println(vector.hashCode());
vector.add("JACK");
vector.add("MARRAY");
vector.add("JACK");
//輸出:[[BILL], JACK, MARRAY, JACK]
System.out.println(vector);
/*
* 返回此向量中第1次出現(xiàn)的指定元素的索引,如果此向量不包括該元素,則返回 ⑴。更確切地講,
* 返回滿足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引 i;
* 如果沒(méi)有這樣的索引,則返回 ⑴。
*/
System.out.println(vector.indexOf("JACK"));//輸出:1
/*
* 返回此向量中第1次出現(xiàn)的指定元素的索引,從 index 處正向搜索,
* 如果未找到該元素,則返回 ⑴。
* 如果指定索引為負(fù)數(shù),拋出IndexOutOfBoundsException異常
*/
System.out.println(vector.indexOf("JACK", 2));//輸出:3
/*
* 將指定對(duì)象作為此向量中的組件插入到指定的 index 處。
* 此向量中的每一個(gè)索引大于等于指定 index 的組件都將向上移位,
* 使其索引值變成比之前大 1 的值。 索引必須為1個(gè)大于等于 0 且
* 小于等于向量當(dāng)前大小的值(如果索引等于向量確當(dāng)前大小,
* 則將新元素添加到向量)。
* 如果索引超越范圍 (index < 0 || index > size()),拋出ArrayIndexOutOfBoundsException異常
*/
vector.insertElementAt("WORD", 1);
//輸出:[[BILL], WORD, JACK, MARRAY, JACK]
System.out.println(vector);
/*
* 當(dāng)且僅當(dāng)此向量沒(méi)有組件(也就是說(shuō)其大小為零)時(shí)返回 true;否則返回 false。
*/
System.out.println(vector.isEmpty());//輸出:false
/*
* 返回此向量中最后1次出現(xiàn)的指定元素的索引;如果此向量不包括該元素,則返回 ⑴。更確切地講,
* 返回滿足 (o==null ? get(i)==null : o.equals(get(i))) 的最高索引 i
* ;如果沒(méi)有這樣的索引,則返回 ⑴。
*/
System.out.println(vector.lastIndexOf("JACK"));//輸出:4
/*
* 移除此向量中指定位置的元素。將所有后續(xù)元素左移(將其索引減 1)。返回此向量中移除的元素。
* 如果索引超越范圍 (index < 0 || index >= size()),拋出ArrayIndexOutOfBoundsException 異常
*/
vector.remove(0);
//輸出:[WORD, JACK, MARRAY, JACK]
System.out.println(vector);
/*
* 移除此向量中指定元素的第1個(gè)匹配項(xiàng),如果向量不包括該元素,則元素保持不變。
* 更確切地講,移除其索引 i 滿足 (o==null ? get(i)==null : o.equals(get(i)))
* 的元素(如果存在這樣的元素)。
*/
System.out.println(vector.remove("WORD"));
System.out.println(vector);//輸出:[JACK, MARRAY, JACK]
}
}
關(guān)于Vector的更多方法,請(qǐng)參看后續(xù)的文章
轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/hai_qing_xu_kong/article/details/44106381 情緒控_
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)