Java快速掌握Vector類方法
Vector的基本介紹
1.:Vector類的定義:
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
2:底層也是一個對象數(shù)組
protected Object[] elementData;
3:Vector是線程同步的,即線程安全,Vector類帶有操作方法有synchronized
4:在開發(fā)中,需要線程安全時,考慮Vector
Vector 類支持 4 種構(gòu)造方法
1 第一種構(gòu)造方法創(chuàng)建一個默認(rèn)的向量,默認(rèn)大小為 10:
public Vector() { this(10); }
第二種構(gòu)造方法創(chuàng)建指定大小的向量。
public Vector(int initialCapacity) { this(initialCapacity, 0); }
第三種構(gòu)造方法創(chuàng)建指定大小的向量,并且增量用 capacityIncrement 指定。增量表示向量每次增加的元素數(shù)目。
/** * Constructs an empty vector with the specified initial capacity and * capacity increment. * * @param initialCapacity the initial capacity of the vector * @param capacityIncrement the amount by which the capacity is * increased when the vector overflows向量溢出時容量增加的量 * @throws IllegalArgumentException if the specified initial capacity * is negative */ public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; }
第四種構(gòu)造方法創(chuàng)建一個包含集合 c 元素的向量:
public Vector(Collection<? extends E> c) { elementData = c.toArray(); elementCount = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, elementCount, Object[].class); }
一些常用的方法
1.add方法
注意:add可以存入一個null;詳見size放法
1.將指定元素添加到此向量的末尾。
boolean add(Object o)
2.在此向量的指定位置插入指定的元素。
void add(int index, Object element)
3.將指定 Collection 中的所有元素添加到此向量的末尾,
按照指定 collection 的迭代器所返回的順序添加這些元素。
boolean addAll(Collection c)
4.在指定位置將指定 Collection 中的所有元素插入到此向量中。
boolean addAll(int index, Collection c)
2.remove方法
1.移除此向量中指定位置的元素。
Object remove(int index)
2.移除此向量中指定元素的第一個匹配項(xiàng),如果向量不包含該元素,
則元素保持不變。
boolean remove(Object o)
3.從此向量中移除包含在指定 Collection 中的所有元素。
boolean removeAll(Collection c)
3.set方法
1.用指定的元素替換此向量中指定位置處的元素。
Object set(int index, Object element)
2.將此向量指定 index 處的組件設(shè)置為指定的對象
void setElementAt(Object obj, int index)
4.size、capacity、get方法
size返回此向量中的組件數(shù)(就是向量存是對象的數(shù)量)。
capacity 返回此向量的當(dāng)前容量。
get 返回第幾個的內(nèi)容
int size();
int capacity();
Object get(int index);
代碼
import java.util.Vector; /** * @autor 笑霸fianl~ * 歡迎訪問GitHub:https://github.com/XBfinal * 歡迎訪問Gitee:https://gitee.com/XBfianl * 歡迎訪問CSDN:https://blog.csdn.net/weixin_52062043 */ public class enumeration01 { public static void main(String[] args) { Vector vector = new Vector(); for(int i=0;i<10;i++){ vector.add(i); } for(int i=0;i<10;i++){ System.out.print(vector.get(i)+"\t"); } vector.add(null);//可以存一個null System.out.println("\n"+"組件數(shù)="+vector.size()); System.out.println("容量="+vector.capacity()); } }
到此這篇關(guān)于Java快速掌握Vector類方法的文章就介紹到這了,更多相關(guān)Java Vector內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Idea之沒有網(wǎng)絡(luò)的情況下創(chuàng)建SpringBoot項(xiàng)目的方法實(shí)現(xiàn)
本文主要介紹了Idea之沒有網(wǎng)絡(luò)的情況下創(chuàng)建SpringBoot項(xiàng)目的方法實(shí)現(xiàn),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09細(xì)說Springcloud eureka的幾種主動下線服務(wù)的方式
這篇文章主要介紹了細(xì)說Springcloud eureka的幾種主動下線服務(wù)的方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Java用單向環(huán)形鏈表來解決約瑟夫環(huán)Josepfu問題
如果把單鏈表的最后一個節(jié)點(diǎn)的指針指向鏈表頭部,而不是指向NULL,那么就構(gòu)成了一個單向循環(huán)鏈表,通俗講就是把尾節(jié)點(diǎn)的下一跳指向頭結(jié)點(diǎn)2021-10-10Java高效實(shí)現(xiàn)電商產(chǎn)品排序?qū)崙?zhàn)
這篇文章主要為大家介紹了Java高效實(shí)現(xiàn)電商產(chǎn)品排序?qū)崙?zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11詳解Java中方法重寫與重載的區(qū)別(面試高頻問點(diǎn))
這篇文章主要介紹了Java中方法重寫與重載的區(qū)別(面試高頻問點(diǎn)),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03解決IntelliJ IDEA創(chuàng)建spring boot無法連接http://start.spring.io/問題
這篇文章主要介紹了解決IntelliJ IDEA創(chuàng)建spring boot無法連接http://start.spring.io/問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08