Tuesday, August 21, 2012

Vector Operations Using Java Programming


import java.util.Iterator;
import java.util.Vector;

public class VectorOperations {

public static void main(String args[]){
Vector vt=new Vector();

vt.add(21);
vt.add(12);
vt.add(57);
vt.add(29);
vt.add(43);

System.out.println("Vector"+vt);

//Adding elements in Vector
vt.add(2, 25);
System.out.println("Vector"+vt);
vt.addElement(23);
System.out.println("Vector"+vt);

//Vector Operations.
System.out.println("Capacity"+vt.capacity());
System.out.println("Conatains 43:"+vt.contains(43));
System.out.println("First element"+vt.firstElement());
System.out.println("Value at Index 4: "+vt.get(4));
System.out.println("Remove value at 3"+vt.remove(3));

vt.add(43);
vt.add(45);
vt.add(46);
vt.add(47);
vt.add(48);
vt.add(49);
vt.add(32);
vt.add(33);
vt.add(34);

System.out.println("Vector Elements afte insertion more than 10 elements");
Iterator it=vt.iterator();
while(it.hasNext())
System.out.print(" "+it.next());
}

}


Output:

Vector[21, 12, 25, 57, 29, 43, 23]
Capacity10
Conatains 43:true
First element21
Value at Index 4: 29
Remove value at 357
Vector Elements afte insertion more than 10 elements
 21 12 25 29 43 23 43 45 46 47 48 49 32 33 34

Queue Operations Using LinkedList in Java



import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;


public class QueueOperations {

public static void main(String args[]){

Queue que=new LinkedList();

//Insert elements into Queue
que.offer(12);
que.offer(25);
que.offer(75);
que.offer(9);

System.out.println("Queue"+que);

//Queue Operations
System.out.println("Queue Peek the elmenet  "+que.peek());
System.out.println("Queue Remove Element "+que.poll());

System.out.println("isEmpty :?"+que.isEmpty());
}
}

Output:
Queue[12, 25, 75, 9]
Queue Peek the elmenet  12
Queue Remove Element 12
isEmpty :?false