Monday, January 12, 2015

Java Using Vector Program



How to read all elements in vector by using Enumeration

package com.java2novice.vector;
import java.util.Enumeration;
import java.util.Vector;
public class VectorEnnumaratio
{
public static void main(String a[])
{
        Vector vct = new Vector();
        vct.add("First");
        vct.add("Second");
        vct.add("Third");
        vct.add("Random");
        Enumeration enm = vct.elements();
        while(enm.hasMoreElements())
       {
            System.out.println(enm.nextElement());
        }
    }
}

How to copy or clone a vector

package com.java2novice.vector;
import java.util.Vector;
public class MyVectorClone
{
public static void main(String a[])
{
 Vector vct = new Vector();
        vct.add("First");
        vct.add("Second");
        vct.add("Third");
        vct.add("Random");
        System.out.println("Actual vector:"+vct);
        Vector copy = (Vector) vct.clone();
        System.out.println("Cloned vector:"+copy);
    }
}

How to add all elements of a list to vector

package com.java2novice.vector;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class MyVectorNewCollection
{
public static void main(String a[])
{
        Vector vct = new Vector();
        vct.add("First");
        vct.add("Second");
        vct.add("Third");
        vct.add("Random");
        System.out.println("Actual vector:"+vct);
        List list = new ArrayList();
        list.add("one");
        list.add("two");
        vct.addAll(list);
        System.out.println("After Copy: "+vct);
    }
}

How to delete all elements from my vector

package com.java2novice.vector;
import java.util.Vector;
public class ClearMyVector
{
public static void main(String a[])
{
        Vector vct = new Vector();
        vct.add("First");
        vct.add("Second");
        vct.add("Third");
        vct.add("Random");
        System.out.println("Actual vector:"+vct);
        vct.clear();
        System.out.println("After clear vector:"+vct);
    }
}

How to find does vector contains all list elements or not

package com.java2novice.vector;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class MyElementCheck
{
public static void main(String a[])
{
        Vector vct = new Vector();
        vct.add("First");
        vct.add("Second");
        vct.add("Third");
        vct.add("Random");
        List list = new ArrayList();
        list.add("Second");
        list.add("Random");
        System.out.println("Does vector contains all list elements?: "+vct.containsAll(list));
        list.add("one");
        System.out.println("Does vector contains all list elements?: "+vct.containsAll(list));
    }
}

No comments:

Post a Comment