Java快速掌握Vector类方法

网友投稿 282 2022-08-23


Java快速掌握Vector类方法

目录Vector的基本介绍Vector类支持4种构造方法一些常用的方法1.add方法2.remove方法3.set方法4.size、capacity、get方法代码

Vector的基本介绍

1.:Vector类的定义:

public class Vector

extends AbstractList

implements List, RandomAccess, Cloneable, java.io.Serializable

2:底层也是一个对象数组

protected Object[] elementData;

3:Vector是线程同步的,即线程安全,Vector类带有操作方法有synchronized

4:在开发中,需要线程安全时,考虑Vector

Vector 类支持 4 种构造方法

1 第一种构造方法创建一个默认的向量,默认大小为 10:

public Vector() {

this(10);

}

第二种构造方法创建指定大小的向量。

public Vector(int initialCapacity) {

this(initialCapacity, 0);

}

第三种构造方法创建指定大小的向量,并且增量用 capacityIncrement 指定。增量表示向量每次增加的元素数目。

/**

* 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;

}

第四种构造方法创建一个包含集合 c 元素的向量:

public Vector(Collection extends E> c) {

elementData = c.toArray();

elemenSlSsvtCount = 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.将指定元素添加到此向量的末尾。booleanhttp:// 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.移除此向量中指定元素的第一个匹配项,如果向量不包含该元素,则元素保持不变。boolean remove(Object o)3.从此向量中移除包含在指定 Collection 中的所有元素。boolean removeAll(Collection c)

3.set方法

1.用指定的元素替换此向量中指定位置处的元素。Object set(int index, Object element)2.将此向量指定 index 处的组件设置为指定的对象void setElementAt(Object obj, int index)

4.size、capacity、get方法

size返回此向量中的组件数(就是向量存是对象的数量)。

capacity 返回此向量的当前容量。

get 返回第几个的内容

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&lthttp://;10;i++){

System.out.print(vector.get(i)+"\t");

}

vector.add(null);//可以存一个null

System.out.println("\n"+"组件数="+vector.size());

System.out.println("容量="+vector.capacity());

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:1161. 最大层内元素和 (二叉树的层序遍历)(11617是什么意思)
下一篇:python 嵌套列表的列表的字符长度排序(python是什么意思)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~