Java快速掌握Vector类方法

作者:笑霸final 时间:2023-11-24 22:49:18 

Vector的基本介绍

1.:Vector类的定义:


public class Vector<E>
   extends AbstractList<E>
   implements List<E>, 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();
       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.移除此向量中指定元素的第一个匹配项,如果向量不包含该元素,
则元素保持不变。
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);

Java快速掌握Vector类方法

代码


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"+"组件数="+vector.size());
       System.out.println("容量="+vector.capacity());
   }
}

来源:https://blog.csdn.net/weixin_52062043/article/details/122814452

标签:Java,Vector,入门
0
投稿

猜你喜欢

  • Android营造雪花和雨滴浪漫效果

    2022-11-14 22:47:40
  • SpringBoot 自定义starter yaml提示失效问题及解决方法

    2022-08-03 14:58:42
  • java反射应用详细介绍

    2022-06-14 04:59:45
  • Java 如何安全的发布对象

    2022-08-09 05:38:52
  • C#通过标签软件Bartender的ZPL命令打印条码

    2022-05-22 01:30:40
  • Java解析json报文实例解析

    2023-07-18 21:33:58
  • SpringBoot实现过滤器和拦截器的方法

    2022-10-21 23:29:34
  • C#微信公众平台开发之access_token的获取存储与更新

    2023-12-16 06:12:04
  • java.net.SocketException: Connection reset 解决方法

    2023-06-17 01:28:43
  • 关于Java float和double精度范围大小

    2023-11-29 00:52:01
  • java.exe和javaw.exe的区别及使用方法

    2022-08-27 10:03:39
  • Java经典面试题最全汇总208道(四)

    2023-11-08 23:59:26
  • java设计模式之中介者模式

    2022-12-08 09:31:25
  • java文件读写工具类分享

    2023-07-30 21:04:19
  • Java RabbitMQ高级特性详细分析

    2021-12-26 00:31:36
  • Spring Boot 实例代码之通过接口安全退出

    2022-02-14 04:41:36
  • Android在linux下刷机教程

    2023-09-03 17:21:04
  • Android实现左右滑动切换图片

    2021-06-20 10:51:30
  • Java分页查询--分页显示(实例讲解)

    2023-02-05 11:07:15
  • Java类中字段可以不赋予初始值的原因分析

    2023-01-05 15:55:49
  • asp之家 软件编程 m.aspxhome.com