java Vector的特点与使用

Vector

Vector的数据结构

基于数组,大小可变(数组扩容)。

Vector与ArrayList的比较

我看了下源码,最大区别就是Vector的方法加了synchronized,是线程安全的

常用方法使用例子

import java.util.Vector;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class VectorTest{
    
    public static void main(String[] args) {
        Vector<String> vec=new Vector<>();
        vec.add("1");
        vec.add("2");
        vec.add("3");
        vec.add("4");
        vec.add("5");

        // 设置第一个元素为10
        vec.add(0,"10");
        //把20放到第二个元素
        vec.insertElementAt("20",1);
        // 将"30"插入到第3个位置
        vec.set(3,"30");

        for(String str: vec){
            log.info("value : {} ,index {}",str,vec.indexOf(str));
        }

        // (顺序查找)获取10的索引
        log.info("value: 10,index {}",vec.indexOf("10"));
        // (倒序查找)获取10的索引
        log.info("value: 10,index {}",vec.lastIndexOf("10"));
        // 获取第一个元素
        log.info("index 0 value is {}",vec.get(0));
        // 获取第3个元素
        log.info("index 0 value is {}",vec.elementAt(0));
        // 获取最后一个元素
        log.info("last index value is", vec.lastElement());
        // 获取Vector的大小
        log.info("vector size is {}",vec.size());
        // 获取Vector的容量
        log.info("vector cap is {}",vec.capacity());
        // 获取vec对应的String数组
        log.info("vector to string  {}",vec.toArray(new String[0]).toString());
        // 清空Vector
        vec.clear();
        log.info("vector size is {}",vec.size());
        log.info("vector cap is {}",vec.capacity());
        // 判断Vector是否为空    
        log.info("vector is empty: {}",vec.isEmpty());
    }

运行结果

2019-07-11 13:36:07,044   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value : 10 ,index 0
2019-07-11 13:36:07,048   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value : 20 ,index 1
2019-07-11 13:36:07,049   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value : 1 ,index 2
2019-07-11 13:36:07,049   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value : 30 ,index 3
2019-07-11 13:36:07,049   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value : 3 ,index 4
2019-07-11 13:36:07,049   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value : 4 ,index 5
2019-07-11 13:36:07,049   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value : 5 ,index 6
2019-07-11 13:36:07,049   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value: 10,index 0
2019-07-11 13:36:07,049   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - value: 10,index 0
2019-07-11 13:36:07,049   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - index 0 value is 10
2019-07-11 13:36:07,050   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - index 0 value is 10
2019-07-11 13:36:07,050   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - last index value is
2019-07-11 13:36:07,050   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - vector size is 7
2019-07-11 13:36:07,050   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - vector cap is 10
2019-07-11 13:36:07,050   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - vector to string  [Ljava.lang.String;@17a7cec2
2019-07-11 13:36:07,051   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - vector size is 0
2019-07-11 13:36:07,051   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - vector cap is 10
2019-07-11 13:36:07,051   [main] INFO  com.fang.java.javabase.firstcollection.VectorTest  - vector is empty: true

posted @ 2019-07-11 13:52  碧海潮心  阅读(2028)  评论(0编辑  收藏  举报