Thursday, October 30, 2014

Java Vector Pro



Add an element at specified index of Java Vector

import java.util.Vector; 
public class AddElementToSpecifiedIndexVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add(1,"Inserted element");
System.out.println("Vector contains...");
for(int index=0; index < v.size(); index++)
System.out.println(v.get(index));
   }
}

Append all elements of other Collection to Java Vector

import java.util.Vector;
import java.util.ArrayList;
public class AppendAllElementsOfOtherCollectionToVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
ArrayList arrayList = new ArrayList();
arrayList.add("4");
arrayList.add("5");
v.addAll(arrayList);
System.out.println("After appending all elements of ArrayList,
Vector contains..");
for(int i=0; i
System.out.println(v.get(i));
   }
}

Copy all elements of Java Vector to an Object Array

import java.util.Vector;
public class CopyElementsOfVectorToArrayExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
Object[] objArray = new Object[5]; v.copyInto(objArray);
System.out.println("Vector elements are copied into an Array.
Now Array Contains..");
for(int index=0; index < objArray.length ; index++)
System.out.println(objArray[index]);
  }
}

Get Size of Java Vector and loop through the elements

import java.util.Vector; 
public class GetSizeOfVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
int totalElements = v.size(); System.out.println("Vector contains...");
for(int index=0; index < totalElements; index++)
System.out.println(v.get(index));
    }
}

Get Sub List of Java Vector

import java.util.Vector;
import java.util.List; 
public class GetSubListOfJavaVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
List lst = v.subList(1,3);
System.out.println("Sub list contains : ");
for(int i=0; i< lst.size() ; i++)
System.out.println(lst.get(i));
Object obj = lst.remove(0);
System.out.println(obj + " is removed from sub list");
System.out.println("After removing " + obj + " from sub list,
original Vector contains : ");
for(int i=0; i< v.size() ; i++)
System.out.println(v.get(i));
  }
}

No comments:

Post a Comment