Here is a code that demos java.util.Vector member methods.
To whom intersested in J.
__________________________________________________
/*
* a program demonstrating the operation of vector member functions
*/
import java.io.*;
import java.util.Vector;
class testVectors {
// main
public static void main(String a[]){
Vector v1 = new Vector();
processVector(v1);
}
//process the vector (format / display / manipulate)
static void processVector(Vector v){
//fill the vector
for ( int i=0; i < 5 ; i++)
v.addElement(new Integer(i)); // [0,1,2,3,4]
System.out.println(" v = "+v);
Integer j = new Integer(3);
// returns the occurences of 3 in vector v , and the occurences from index 4
System.out.println(v.indexOf(j)+" "+v.indexOf(j,4));
// returns whether v contains 3 , and last index of 3
System.out.println(v.contains(j)+" "+v.lastIndexOf(j));
// new vector v2
Vector v2 = new Vector(3,4); // capacity=3 capacity increment=4
// fill the vector v2
for(int i=4 ; i <8 ; i++)
v2.addElement(new Integer(i)); // [4,5,6,7,8]
// extend the size of the vector to accomodate at least specified objects.
v2.ensureCapacity(9);
Vector v3 = new Vector(2); // creates a vec capacity of 2
v3.setSize(4); // size = capacity = 4 v3 = [null,null,null,null]
v3.setElementAt(new Integer(9), 1); // [null,9,null,null]
v3.setElementAt(new Integer(3), 3); // [null,9,null,3]
// insert a element at given position after shifting elements at positions
// following the giben position by one.
v3.insertElementAt(v3.elementAt(3),1); // [null ,3,9,null,3]
v3.ensureCapacity(9);
v3.removeElement(new Integer(9)); // [null,3,null,3]
v3.removeElementAt( v3.size()- 2); // [null,3,3]
System.out.println("v3 "+v3);
//elements() returns an Enumeration obj that enumerates all the
// objects in the vector
java.util.Enumeration ev = v3.elements();
while(ev.hasMoreElements())
System.out.println(ev.nextElement()+ "");
System.out.println();
v3.removeElementAt(0); // [3,3]
//add all collection v
v3.addAll(v); // [3,3,0,1,2,3,4]
// removes the contains of v2
v3.removeAll(v2); // [3,3,0,1,2,3]
// adds the collection to a specified position
v3.addAll(2,v) ; // [3, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3]
// removes the objects which are not int the collection specified
// in this case objects not in v2 will be removed from v3
v3.retainAll(v2); // [4] , intersection(v3,v2)
v.subList(1,3).clear(); // [0,3,4]
Vector v4 = new Vector() , v5 ;
v4.addElement( new Node("Jill",23));
v5 = (Vector)v4.clone(); // v4 = [("Jill",23)]
((Node)v4.firstElement()).age = 34; // v4 = v5 = [("Jill",34)]
System.out.println("v4 "+v4+" \n v5 "+v5);
/******************************************************************/
/* The method clone() should be used carefully. */
/* It clones the array implementing the vector */
/* but not THE OBJECTS OF ARRAY . */
/* after method finished the cloned vector includes references to */
/* the same objects as the vector from which it was cloned. */
/******************************************************************/
}
}
class Node {
String name;
int age;
Node(String s , int n){
this.name=s;
this.age=n;
}
public String toString(){
return "("+this.name+" , "+this.age+")";
}
}
To whom intersested in J.
__________________________________________________
/*
* a program demonstrating the operation of vector member functions
*/
import java.io.*;
import java.util.Vector;
class testVectors {
// main
public static void main(String a[]){
Vector v1 = new Vector();
processVector(v1);
}
//process the vector (format / display / manipulate)
static void processVector(Vector v){
//fill the vector
for ( int i=0; i < 5 ; i++)
v.addElement(new Integer(i)); // [0,1,2,3,4]
System.out.println(" v = "+v);
Integer j = new Integer(3);
// returns the occurences of 3 in vector v , and the occurences from index 4
System.out.println(v.indexOf(j)+" "+v.indexOf(j,4));
// returns whether v contains 3 , and last index of 3
System.out.println(v.contains(j)+" "+v.lastIndexOf(j));
// new vector v2
Vector v2 = new Vector(3,4); // capacity=3 capacity increment=4
// fill the vector v2
for(int i=4 ; i <8 ; i++)
v2.addElement(new Integer(i)); // [4,5,6,7,8]
// extend the size of the vector to accomodate at least specified objects.
v2.ensureCapacity(9);
Vector v3 = new Vector(2); // creates a vec capacity of 2
v3.setSize(4); // size = capacity = 4 v3 = [null,null,null,null]
v3.setElementAt(new Integer(9), 1); // [null,9,null,null]
v3.setElementAt(new Integer(3), 3); // [null,9,null,3]
// insert a element at given position after shifting elements at positions
// following the giben position by one.
v3.insertElementAt(v3.elementAt(3),1); // [null ,3,9,null,3]
v3.ensureCapacity(9);
v3.removeElement(new Integer(9)); // [null,3,null,3]
v3.removeElementAt( v3.size()- 2); // [null,3,3]
System.out.println("v3 "+v3);
//elements() returns an Enumeration obj that enumerates all the
// objects in the vector
java.util.Enumeration ev = v3.elements();
while(ev.hasMoreElements())
System.out.println(ev.nextElement()+ "");
System.out.println();
v3.removeElementAt(0); // [3,3]
//add all collection v
v3.addAll(v); // [3,3,0,1,2,3,4]
// removes the contains of v2
v3.removeAll(v2); // [3,3,0,1,2,3]
// adds the collection to a specified position
v3.addAll(2,v) ; // [3, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3]
// removes the objects which are not int the collection specified
// in this case objects not in v2 will be removed from v3
v3.retainAll(v2); // [4] , intersection(v3,v2)
v.subList(1,3).clear(); // [0,3,4]
Vector v4 = new Vector() , v5 ;
v4.addElement( new Node("Jill",23));
v5 = (Vector)v4.clone(); // v4 = [("Jill",23)]
((Node)v4.firstElement()).age = 34; // v4 = v5 = [("Jill",34)]
System.out.println("v4 "+v4+" \n v5 "+v5);
/******************************************************************/
/* The method clone() should be used carefully. */
/* It clones the array implementing the vector */
/* but not THE OBJECTS OF ARRAY . */
/* after method finished the cloned vector includes references to */
/* the same objects as the vector from which it was cloned. */
/******************************************************************/
}
}
class Node {
String name;
int age;
Node(String s , int n){
this.name=s;
this.age=n;
}
public String toString(){
return "("+this.name+" , "+this.age+")";
}
}