java中Vector有什么用

 Vector的用法

马克-to-win:Vector和ArrayList很类似,前面讲了二者的区别,就在于同步的问题上,Vector的使用是非常简单的:先创建一个,再用addElement()置入对象,以后用elementAt()取得那些对象

例:3.1.1

import java.io.*;
import java.util.*;

public class TestMark_to_win {
    public static void main(String args[]) {
        Vector v = new Vector();
        for (int i = 0; i < 4; i++) {
            v.addElement(new Integer(i));
        }
        Iterator it = v.iterator();
        while (it.hasNext()) {
            System.out.println("" + it.next());
        }
        if (v.contains(new Integer(2))) {
            System.out.println("it has 2");
        }
        Enumeration e = v.elements();
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
        v.removeElementAt(2);
        v.addElement("kkk");
        v.addElement(new Double(1.1));
        /*the following statement is a must, because e has ever gone to the end
of the queue.*/
        e = v.elements();
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
    }
}

result is:

0
1
2
3
it has 2
0
1
2
3
0
1
3
kkk
1.1