Using Vectors

5 April 2007

When using arrays, you have to give their size at declaration. So arrays do not expand dynamically. Vectors expand automatically when data is added to them. Vector class is included in java.util package. Vectors are used to store Objects.

Vector is thread safe. It means Vectors are synchronized, means you can use Vectors even if your object might be shared by two or more threads at a same time. This makes Vector slower than ArrayList which is not thread safe.

Following examples will clear your concepts.

import java.util.*;
public class TestVectorClass {
 
	public static void main(String[] str)
	{
 
		Vector myVector = new Vector();
 
		Student obj1 = new Student("Dave",25,"Databases");
		Student obj2 = new Student("Morisson",27,"Java");
		Student obj3 = new Student("Adnan",26,"Softwares");
 
		String str_country = new String("England");
 
// adding objects of Student class in Vector
		myVector.add(obj1);
		myVector.add(obj2);
		myVector.add(obj3);
 
// adding a String object in Vector
		myVector.add(str_country);
 
// temp object created
		Student temp = new Student();
 
// retriving 2nd object of Student class
		temp = (Student)myVector.elementAt(1);
 
// Moission's record will be printed
		System.out.println("Name: " + temp.getStr_name());
		System.out.println("Age: " + temp.getInt_age());
		System.out.println("Major: " + temp.getStr_major());
 
	}
}
 
public class Student {
	private String str_name;
	private int int_age;
	private String str_major;
 
// constructor
	public Student(String str_name, int int_age,String str_major)
	{
		this.str_name = str_name;
		this.int_age = int_age;
		this.str_major = str_major;
	}
 
	public Student()
	{
	}
	public int getInt_age() {
		return int_age;
	}
 
	public void setInt_age(int int_age) {
		this.int_age = int_age;
	}
 
	public String getStr_major() {
		return str_major;
	}
 
	public void setStr_major(String str_major) {
		this.str_major = str_major;
	}
 
	public String getStr_name() {
		return str_name;
	}
 
	public void setStr_name(String str_name) {
		this.str_name = str_name;
	}
 
}
 
// You can also use ListIterator to iterate through all the List elements.
 
		Vector myVector = new Vector();
		String str_country = new String("England");
		myVector.add(str_country);
		str_country = "Australia";
		myVector.add(str_country);
		str_country = "Germany";
		myVector.add(str_country);
 
		// vector class myVector has 3 String objects in it
 
		// using ListIterator to iterate through all elements
		ListIterator iter = myVector.listIterator();
		while (iter.hasNext()) {
			System.out.println((String)iter.next());
}

del.icio.us:Using Vectors  digg:Using Vectors  spurl:Using Vectors  wists:Using Vectors  simpy:Using Vectors  newsvine:Using Vectors  blinklist:Using Vectors  furl:Using Vectors  reddit:Using Vectors  fark:Using Vectors  blogmarks:Using Vectors  Y!:Using Vectors  smarking:Using Vectors  magnolia:Using Vectors  segnalo:Using Vectors  gifttagging:Using Vectors

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Using Vectors

Leave a Reply