Java集合框架学习(七) Vector详解
Vector介绍
Vector 实现了List接口。和ArrayList一样也维护元素的插入顺序。
但它一般只用在多线程环境,因为它是线程同步的。
还有就是它对元素的增删改查效率低下。
类定义
public class Vector<E>extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
有三种构造方法来实例化Vector
1.Vector vec = new Vector();
2.Vector object= new Vector(int initialCapacity)
Vector vec = new Vector(3); //出事大小为33.Vector object= new vector(int initialcapacity, capacityIncrement)
Vector vec= new Vector(4, 6); //初始大小为4,后面每次扩展6个空间
常用方法
增:add, addAll,
删:clear, remove
改:set,
查:capacity, contains, containsAll, elements, firstElement, get, indexOf等
转换:toArray, toString例子介绍
package com.dylan.collection;
import java.util.Enumeration;
import java.util.Vector;
/**
* @author xusucheng
* @create 2018-01-31
**/
public class VectorExample {
public static void main(String args[]) {
/* Vector of initial capacity(size) of 2 */
Vector<String> vec = new Vector<String>(2);
/* Adding elements to a vector*/
vec.addElement("Apple");
vec.addElement("Orange");
vec.addElement("Mango");
vec.addElement("Fig");
/* check size and capacityIncrement*/
System.out.println("Size is: "+vec.size());
System.out.println("Default capacity increment is: "+vec.capacity());
vec.addElement("fruit1");
vec.addElement("fruit2");
vec.addElement("fruit3");
/*size and capacityIncrement after two insertions*/
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after increment is: "+vec.capacity());
/*Display Vector elements*/
Enumeration en = vec.elements();
System.out.println("\nElements are:");
while(en.hasMoreElements())
System.out.print(en.nextElement() + " ");
}
}
如何给Vector排序
package com.dylan.collection;
import java.util.Collections;
import java.util.Vector;
/**
* @author xusucheng
* @create 2018-01-31
**/
public class SortingVectorExample {
public static void main(String[] args) {
// Create a Vector
Vector<String> vector = new Vector<String>();
//Add elements to Vector
vector.add("Walter");
vector.add("Anna");
vector.add("Hank");
vector.add("Flynn");
vector.add("Tom");
// By Default Vector maintains the insertion order
System.out.println("Vector elements before sorting: ");
for(int i=0; i < vector.size(); i++){
//get(i) method fetches the element from index i
System.out.println(vector.get(i));
}
// Collection.sort() sorts the collection in ascending order
Collections.sort(vector);
//Display Vector elements after sorting using Collection.sort
System.out.println("Vector elements after sorting: :");
for(int i=0; i < vector.size(); i++){
System.out.println(vector.get(i));
}
}
}
Vector转换为ArrayList
package com.dylan.collection;
import java.util.ArrayList;
import java.util.Vector;
/**
* @author xusucheng
* @create 2018-01-31
**/
public class VectorToArrayList {
public static void main(String[] args) {
// Creating a Vector of String elements
Vector<String> vector = new Vector<String>();
// Populate Vector
vector.add("Rahul");
vector.add("Steve");
vector.add("Jude");
vector.add("Locke");
vector.add("Mike");
vector.add("Robert");
//Displaying Vector elements
for (String str : vector){
System.out.println(str);
}
//Converting Vector to ArrayList
ArrayList<String> arraylist = new ArrayList<String>(vector);
//Displaying ArrayList Elements
System.out.println("\nArrayList Elements :");
for (String s : arraylist){
System.out.println(s);
}
}
}
Vector转换为Array
package com.dylan.collection;
import java.util.Vector;
/**
* @author xusucheng
* @create 2018-01-31
**/
public class VectorToArray {
public static void main(String[] args) {
// Creating a Vector of String elements
Vector<String> vector = new Vector<String>();
// Add elements to Vector
vector.add("Item1");
vector.add("Item2");
vector.add("Item3");
vector.add("Item4");
vector.add("Item5");
vector.add("Item6");
//Converting Vector to String Array
String[] array = vector.toArray(new String[vector.size()]);
//Displaying Array Elements
System.out.println("String Array Elements :");
for(int i=0; i < array.length ; i++){
System.out.println(array[i]);
}
}
}