create Vector from array - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

create Vector from array

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    public static void main(String[] argv) {
        Object[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(createVector(array));
    }/*from   w  ww.j a  v  a 2s .c  om*/

    public static Vector createVector(Object[] array) {
        return appendInto(new Vector(), array);
    }

    public static Vector createVector(Enumeration e) {
        Vector result = new Vector();
        while (e.hasMoreElements())
            result.addElement(e.nextElement());
        return result;
    }

    public static Vector appendInto(Vector v, Object[] array) {
        appendInto(v, array, array.length);
        return v;
    }

    public static Vector appendInto(Vector v, Object[] array, int quantity) {
        v.ensureCapacity(v.size() + quantity);
        for (int i = 0; i < quantity; i++) {
            v.addElement(array[i]);
        }

        return v;
    }

    public static Vector appendInto(Vector dest, Vector src) {
        Enumeration e = src.elements();
        while (e.hasMoreElements())
            dest.addElement(e.nextElement());
        return dest;
    }
}

Related Tutorials