Saturday, April 22, 2017

Java Vector Class with Examples

A Vector class also stores elements similar to ArrayList,but Vector is Synchronized.Vector class implements the List interface.The underlying Data Structure is re-sizable array or growable array. Every method present in the Vector class is synchronized and vector object is Thread-safe.

Creating Vector Class:

1) Vector v=new Vector();//creates an empty vector object with default initital capacity 10
                                                       (OR)
2) Vector v=new Vector(int initialCapacity);//creates a vector 'object' with the specified capacity.Whenever an element is added to the initial capacity,the capacity of the vector is doubled.
 Like this:  new capacity=currentcapcity*2
                                                        (OR)
3) Vector v=new Vector(int initialCapacity,int incrementCapacity)

Vector specific methods:

boolean add(E e)   -------->  Appends the element e to the end of the list

void add(int i,E element)--------> inserts the element e at the specified index 'i'in the list

void addElement(Object 0)------>inserts the element to the vector and increses the vector size by 1,This method is synchronized.

To REMOVE objects:

element remove(int index)  -----> This method removes the element at the specified position in the vector.This method also returns the element which was removed from the Vector.

boolean remove(Object o)  ------> This method removes the first occurrence of the specified element object from the vector,if it is present

void clear() -----> This method removes all the elements from the vector.

To Access Elements:

Object get(int index) -----> This method returns the element available at the specified position in the vector

int size() -----> This method returns the number of elements present in the Vector.

Example:
import java.util.*;
class VectorDemo
{
public static void main(String arg[])
{
Vector v = new Vector();
System.out.println(v.capacity());
for (int i = 0;i<10 ;i++ )
{
v.addElement(i);
}
System.out.println(v.capacity());
v.addElement("Aa");
System.out.println(v.capacity());
System.out.println(v);
}
}
Output:







Example 2:

This example shows creating a vector with Integer elements

import java.util.*;
class VectorTest
{
public static void main(String args[])
{
Vector<Integer>v=new Vector<Integer>();
//take an int type array
int x[]={12,23,34,45,55};
for(int i=0;i<x.length;i++)
{
v.add(x[i]);
}
//retrieve the elements using get()
System.out.println("vector elements");
for(int i=0;i<v.size();i++)
{
System.out.println(v.get(i));
}
//retrieve using ListIterator
System.out.println("Elements using ListIterator");
ListIterator li=v.listIterator();
System.out.println("In forward direction:");
while(li.hasNext())
System.out.println(li.next()+"\t");
System.out.println("Backward Direction:");
while(li.hasPrevious())
System.out.println(li.previous()+"\t");
}
}
Output:











ALSO READ:
How to prepare for campus interviews
Java for each loop with example
How Java PROGRAM WORKS
Java Object class methods with examples

                                                      

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...