Java教程

Java Vector

Vector 就像 动态数组一样,它可以增大或缩小其大小。与数组不同,由于没有大小限制,我们可以在其中存储n个元素。自Java 1.2以来,它是Java Collection框架的一部分。它位于 java.util 包中,并实现了 List 接口,因此我们可以在此处使用List接口的所有方法。
建议仅在线程安全的实现中使用Vector类。如果不需要使用线程安全的实现,则应使用ArrayList,在这种情况下ArrayList的性能会更好。
Vector类返回的迭代器是 fail-fast 。如果进行并发修改,它将失败并引发ConcurrentModificationException。
它与ArrayList类似,但有两个区别-
Vector是同步。 Java Vector包含许多不属于集合框架的遗留方法。

Java Vector类声明

public class Vector<E> extends Object<E> implements List<E>, Cloneable, Serializable

Java向量构造器

向量类支持四种类型的构造器。这些内容如下:
构造函数 说明
vector() 它将构造一个空Vector,默认大小为10。
vector(int initialCapacity) 它构造一个具有指定初始容量且容量增量等于零的空向量。
vector(int initialCapacity,int CapacityIncrement) 它将构造一个具有指定初始容量和容量增量的空向量。
Vector( Collection c) 它构造一个包含集合c的元素的向量。

Java Vector 方法

以下是Vector类方法的列表:
方法 说明
add() 用于将指定元素附加到给定向量中。
addAll() 用于将指定集合中的所有元素附加到此Vector的末尾。
addElement() 用于将指定的组件附加到此向量的末尾。它会使向量大小增加一。
capacity() 用于获取此向量的当前容量。
clear() 它用于删除此向量中的所有元素。
clone() 它将返回此向量的副本。
contains() 如果向量包含指定的元素,则返回true。
containsAll() 如果向量包含指定集合中的所有元素,则返回true。
copyInto() 用于将向量的分量复制到指定的数组中。
elementAt() 用于获取指定索引处的组件。
elements() 它返回向量组成部分的枚举。
ensureCapacity() 如有必要,用于增加正在使用的向量的容量。这样可以确保向量至少可以容纳最小容量参数指定的分量数。
equals() 用于比较指定对象和向量是否相等。
firstElement() 用于获取向量的第一个分量。
forEach() 它用于对Iterable的每个元素执行给定的操作,直到处理完所有元素或该操作引发异常为止。
get() 用于获取向量中指定位置的元素。
hashCode() 用于获取向量的哈希码值。
indexOf() 它用于获取向量中指定元素首次出现的索引。如果向量不包含元素,则返回-1。
insertElementAt() 用于将指定对象作为组件插入给定Vector中指定索引处。
isEmpty() 用于检查此向量是否不包含任何分量。
iterator() 用于以适当的顺序获取列表中元素的迭代器。
lastElement() 它用于获取向量的最后一个分量。
lastIndexOf() 它用于获取向量中指定元素的最后一次出现的索引。如果向量不包含元素,则返回-1。
listIterator() 用于以适当的顺序获取列表中元素的列表迭代器。
remove() 用于从向量中删除指定的元素。如果向量不包含元素,则它保持不变。
removeAll() 用于从向量中删除指定集合中存在的所有元素。
removeAllElements() 用于从向量中删除所有元素,并将向量的大小设置为零。
removeElement() 它用于从向量中删除参数的第一个(索引最低的)出现。
removeElementAt() 用于删除指定索引处的组件。
removeIf() 用于删除集合中满足给定谓词的所有元素。
removeRange() 用于删除向量中索引在fromIndex(包括)和toIndex(不包括)之间的所有元素。
replaceAll() 用于将列表中的每个元素替换为将运算符应用于该元素的结果。
retainAll() 仅用于保留指定集合中包含的向量中的该元素。
set() 用于将向量中指定位置的元素替换为指定元素。
setElementAt() 用于将向量的指定索引处的组件设置为指定对象。
setSize() 它用于设置给定向量的大小。
size() 用于获取给定向量中的分量数。
sort() 它用于根据指定的比较器引发的顺序对列表进行排序。
spliterator() 它用于在列表中的元素上创建后绑定和故障快速的Spliterator。
subList() 它用于查看fromIndex(包括)和toIndex(不包括)之间的列表部分。
toArray() 用于获取array包含此向量中所有元素的正确顺序。
toString() 用于获取向量的字符串表示形式。
trimToSize() 用于将向量的容量调整为向量的当前大小。

Java Vector 示例

import java.util.*;
public class VectorExample {
    public static void main(String args[]) {
        Vector<String> vec = new Vector<String>();
        vec.add("Tiger");
        vec.add("Lion");
        vec.add("Dog");
        vec.add("Elephant");
        vec.addElement("Rat");
        vec.addElement("Cat");
        vec.addElement("Deer");
        System.out.println("Elements are: "+vec);
    }
}
输出:
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]

Java向量示例2

import java.util.*;
public class VectorExample1 {
    public static void main(String args[]) {
        Vector<String>vec = new Vector<String>(4);
        vec.add("Tiger");
        vec.add("Lion");
        vec.add("Dog");
        vec.add("Elephant");
        System.out.println("Size is: "+vec.size());
        System.out.println("default capacity is: "+vec.capacity());
        System.out.println("Vector element is: "+vec);
        vec.addElement("Rat");
        vec.addElement("Cat");
        vec.addElement("Deer");
        System.out.println("Size after addition: "+vec.size());
        System.out.println("Capacity after addition is: "+vec.capacity());
        System.out.println("Elements are: "+vec);
        if(vec.contains("Tiger")){
            System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
        }
        else
            {
            System.out.println("Tiger is not present in the list.");
        }
        //Get the first element
   System.out.println("The first animal of the vector is = "+vec.firstElement());
        //Get the last element
   System.out.println("The last animal of the vector is = "+vec.lastElement());
    }
}
输出:
Size is: 4
default capacity is: 4
Vector element is: [Tiger, Lion, Dog, Elephant]
Size after addition: 7
Capacity after addition is: 8
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]
Tiger is present at the index 0
The first animal of the vector is = Tiger
The last animal of the vector is = Deer

Java Vector 示例3

import java.util.*;
public class VectorExample2 {
    public static void main(String args[]) {
        Vector<Integer> in = new Vector<>();
in.add(100);
        in.add(200);
        in.add(300);
        in.add(200);
        in.add(400);
        in.add(500);
        in.add(600);
        in.add(700);
        //Display the vector elements
System.out.println("Values in vector: " +in);
        //use remove() method to delete the first occurence of an element
System.out.println("Remove first occourence of element 200: "+in.remove((Integer)200));
        //Display the vector elements afre remove() method
System.out.println("Values in vector: " +in);
        //Remove the element at index 4
System.out.println("Remove element at index 4: " +in.remove(4));
        System.out.println("New Value list in vector: " +in);
        //Remove an element
in.removeElementAt(5);
        //Checking vector and displays the element
System.out.println("Vector element after removal: " +in);
        //Get the hashcode for this vector
System.out.println("Hash code of this vector = "+in.hashCode());
        //Get the element at specified index
System.out.println("Element at index 1 is = "+in.get(1));
    }
}
输出:
Values in vector: [100, 200, 300, 200, 400, 500, 600, 700]
Remove first occourence of element 200: true
Values in vector: [100, 300, 200, 400, 500, 600, 700]
Remove element at index 4: 500
New Value list in vector: [100, 300, 200, 400, 600, 700]
Vector element after removal: [100, 300, 200, 400, 600]
Hash code of this vector = 130123751
Element at index 1 is = 300

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4