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类方法的列表:
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