Thursday, October 30, 2014

Java Example Pro



Remove specified element from Java Vector

import java.util.Vector; 
public class RemoveElementFromVectorByObjectExample
{
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");
boolean blnElementFound = v.remove("2");
System.out.println("Was 2 removed from Vector ? " + blnElementFound);
System.out.println("Vector contents after remove call...");
for(int index=0; index < v.size(); index++)
System.out.println(v.get(index));
v.removeElement("3");
System.out.println("Was 3 removed from Vector ? " + blnElementFound);
System.out.println("Vector contents after removeElement call...");
for(int index=0; index < v.size(); index++)
System.out.println(v.get(index)); 
   }
}

Replace an element at specified index of Java Vector

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

Search an element of Java Vector

import java.util.Vector;
public class SearchAnElementInVectorExample
          {
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");
v.add("1");
v.add("2");
boolean blnFound = v.contains("3");
System.out.println("Does Vector contain 3 ? " + blnFound);
int index = v.indexOf("5");
if(index == -1)
System.out.println("Vector does not contain 5");
else
System.out.println("Vector contains 5 at index :" + index);
int lastIndex = v.lastIndexOf("2");
if(lastIndex == -1)
System.out.println("Vector does not contain 2");
else
System.out.println("Last occurrence of 2 in Vector is at index :" + lastIndex);
      }  
}

Search an element of Java Vector from specific index

import java.util.Vector;
public class SearchAnElementFromSpecificIndexVectorExample
{
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");
v.add("1");
v.add("2");
int index = v.indexOf("1",4);
if(index == -1)
System.out.println("Vector does not contain 1 after index # 4");
else
System.out.println("Vector contains 1 after index # 4 at index #" + index);
int lastIndex = v.lastIndexOf("2" , 5);
if(lastIndex == -1)
System.out.println("Vector does not contain 2 after index # 5");
else
System.out.println("Last occurrence of 2  after index # 5 in Vector
is at index #" + lastIndex);
   }  
}

Set size of Java Vector

import java.util.Vector;
public class SetSizeOfJavaVectorExample
{
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");
v.setSize(3);
System.out.println("Vector elements after setting size to 3");
for(int index=0; index < v.size(); index++)
System.out.println(v.get(index));
v.setSize(5);
System.out.println("Vector elements after setting size to 5");
for(int index=0; index < v.size(); index++)
System.out.println(v.get(index));  
  }
}

No comments:

Post a Comment