[JAVA] A Guide to Using Vectors 11-18-2012, 12:46 PM
#1
Java vectors have a big advantage over the array data structure since it is very flexible and its size can be increased or decreased very easily.
Importing Vector
Vectors are a part of java.util package so you need to import it before using it.
Creating a new Vector
One following constructors are to be used to create a Vector.
Example :
This will create a new Vector that has a size of 5 i.e. it can store 5 pieces of information. If you try to store a sixth piece, it will automatically re-size to 8 (5+3). If you add nine, it'll re-size to 12, and so on.
Adding Elements to a Vector
The two basic ways of adding elements to a vector is by using the following methods
The first function appends the element to the end of the vector while the second element adds the element at specific position while resizing the Vector and shifting the remaining elements.
Example :
Removing Elements from a Vector
Removing element(s) from a Vector is very simple. The methods to be used are as follows :
The above method will remove the element from the specified index and return the value of the element that was removed. After removing the element, it will shift the remaining elements to fill up the voids.
The above method will search the entire vector for the element passed as argument, remove its first occurrence, and shift the remaining elements to fill the void created in Vector.
It will remove all the elements within the given lower bound and upper bound indexes and shift the remaining values respectively.
Example :
Reading the Elements in a Vector
Use the following methods to get the elements stored in a Vector
Use the first method to get the element stored at index 0. The second method is used to return the last object of the vector. The other method can be used to retrieve elements from any index in the Vector.
Example :
Check if a Vector is empty or not
Following is the method to check if the Vector is empty or not.
Example :
Some more useful methods of Vector class
The above function returns the index of first occurrence of the element passed as parameter.
The above function returns the index of first occurrence of the element passed as parameter beginning the search from the index passed as second parameter.
The above function returns the index of last occurrence of the element passed as parameter. That means the search starts at the end point of the vector and goes backward.
The above function searches backwards for the specified object, starting from the specified index, and returns an index to it.
It re-sizes the vector to a new size passed as its parameter.
Returns a string representation of this Vector, containing the String representation of each element.
Removes all of the elements from this Vector.
A Sample Program
That brings an end to this tutorial of using Vectors in Java.
Importing Vector
Vectors are a part of java.util package so you need to import it before using it.
Code:
import java.util.Vector;
Creating a new Vector
One following constructors are to be used to create a Vector.
Code:
public Vector()
public Vector(int size)
public Vector(int size,int incr)
public Vector(Collection c)
Example :
Code:
Vector v=new Vector(5,3);
This will create a new Vector that has a size of 5 i.e. it can store 5 pieces of information. If you try to store a sixth piece, it will automatically re-size to 8 (5+3). If you add nine, it'll re-size to 12, and so on.
Adding Elements to a Vector
The two basic ways of adding elements to a vector is by using the following methods
Code:
public boolean add (Object o)
public void add (int index, Object o)
The first function appends the element to the end of the vector while the second element adds the element at specific position while resizing the Vector and shifting the remaining elements.
Example :
Code:
v.add("Hack Community");
v.add(2,"HC");
Removing Elements from a Vector
Removing element(s) from a Vector is very simple. The methods to be used are as follows :
Code:
public Object remove(int index)
The above method will remove the element from the specified index and return the value of the element that was removed. After removing the element, it will shift the remaining elements to fill up the voids.
Code:
public void remove(Object element)
The above method will search the entire vector for the element passed as argument, remove its first occurrence, and shift the remaining elements to fill the void created in Vector.
Code:
void removeRange(int lowerbound,int upperbound)
It will remove all the elements within the given lower bound and upper bound indexes and shift the remaining values respectively.
Example :
Code:
String s=v.remove(2);
v.remove("Hack Community");
v.remove(3,10);
Reading the Elements in a Vector
Use the following methods to get the elements stored in a Vector
Code:
Object firstElement()
Object lastElement()
Object get(int index)
Use the first method to get the element stored at index 0. The second method is used to return the last object of the vector. The other method can be used to retrieve elements from any index in the Vector.
Example :
Code:
String first=v.firstElement();
String fourth=v,get(3);
Check if a Vector is empty or not
Following is the method to check if the Vector is empty or not.
Code:
boolean isEmpty()
Example :
Code:
if(v.isEmpty())
{
System.out.println("The Vector is empty.");
}
else
{
System.out.println("The Vector is not empty");
}
Some more useful methods of Vector class
Code:
int indexOf(Object element)
The above function returns the index of first occurrence of the element passed as parameter.
Code:
int indexOf(Object element,int index)
The above function returns the index of first occurrence of the element passed as parameter beginning the search from the index passed as second parameter.
Code:
int lastIndexOf(Object element)
The above function returns the index of last occurrence of the element passed as parameter. That means the search starts at the end point of the vector and goes backward.
Code:
int lastIndexOf(Object element,int index)
The above function searches backwards for the specified object, starting from the specified index, and returns an index to it.
Code:
void setSize(int newSize)
Code:
String toString()
Returns a string representation of this Vector, containing the String representation of each element.
Code:
void clear()
Removes all of the elements from this Vector.
A Sample Program
Code:
import java.util.*;
class VectorDemo {
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector v = new Vector(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " +
v.capacity());
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
System.out.println("Capacity after four additions: " +
v.capacity());
v.addElement(new Double(5.45));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Float(9.4));
v.addElement(new Integer(10));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("First element: " +
(Integer)v.firstElement());
System.out.println("Last element: " +
(Integer)v.lastElement());
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");
// enumerate the elements in the vector.
Enumeration vEnum = v.elements();
System.out.println("\nElements in vector:");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
}
}
//Source : Java Examples
That brings an end to this tutorial of using Vectors in Java.
Folow me on My YouTube Channel if you're into art.