Vector example in Java

By: Grenfel Viewed: 157435 times    

Vector implements a dynamic array. It is similar to ArrayList, but with two differences:  Vector is synchronized, and it contains many legacy methods that are not part of the collections framework. With the release of Java 2, Vector was reengineered to extend AbstractList and implement the List interface, so it now is fully compatible with collections.

Here are the Vector constructors:

Vector( )
Vector(int size)
Vector(int size, int incr)
Vector(Collection c)

The first form creates a default vector, which has an initial size of 10. The second form creates a vector whose initial capacity is specified by size. The third form creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward. The fourth form creates a vector that contains the elements of collection c. This constructor was added by Java 2.

All vectors start with an initial capacity. After this initial capacity is reached, the next time that you attempt to store an object in the vector, the vector automatically allocates space for that object plus extra room for additional objects. By allocating more than just the required memory, the vector reduces the number of allocations that must take place. This reduction is important, because allocations are costly in terms of time. The amount of extra space allocated during each reallocation is determined by the increment that you specify when you create the vector. If you don't specify an increment, the vector's size is doubled by each allocation cycle.

Vector defines these protected data members:

int capacityIncrement;
int elementCount;
Object elementData[ ];

The increment value is stored in capacityIncrement. The number of elements currently in the vector is stored in elementCount. The array that holds the vector is stored in elementData.

Because Vector implements List, you can use a vector just like you use an ArrayList instance. You can also manipulate one using its legacy methods. For example, after you instantiate a Vector, you can add an element to it by calling addElement( ). To obtain the element at a specific location, call elementAt( ). To obtain the first element in the vector, call firstElement( ). To retrieve the last element, call lastElement( ). You can obtain the index of an element by using indexOf( ) and lastIndexOf( ). To remove an element, call removeElement( ) or removeElementAt( ).

The following program uses a vector to store various types of numeric objects. It demonstrates several of the legacy methods defined by Vector. It also demonstrates the Enumeration interface.

//Demonstrate various Vector operations.
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();
}
}

The output from this program is shown here:

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.
Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12

With the release of Java 2, Vector adds support for iterators. Instead of relying on an enumeration to cycle through the objects (as the preceding program does), you now can use an iterator. For example, the following iterator-based code can be substituted into the program:

// use an iterator to display contents
Iterator vItr = v.iterator();
System.out.println("\\nElements in vector:");
while(vItr.hasNext())
System.out.print(vItr.next() + " ");
System.out.println();

Because enumerations are not recommended for new code, you will usually use an iterator to enumerate the contents of a vector. Of course, much legacy code exists that employs enumerations. Fortunately, enumerations and iterators work in nearly the same manner.

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Good To understand.
View Tutorial          By: Radhika at 2010-05-11 01:24:55

2. Awful formatting, difficult to read.
View Tutorial          By: Gaz at 2010-08-22 16:48:26

3. A very good demonstration o Vector!!
View Tutorial          By: Pankaj Kumar Mishra at 2010-12-09 01:35:06

4. i need table example!!!...
View Tutorial          By: Sadyog at 2011-01-03 21:59:09

5. Good one!!!!!!!!
View Tutorial          By: Manya at 2011-06-30 11:59:25

6. Good example for vector
View Tutorial          By: mangai at 2011-07-07 09:46:10

7. thanks it is nice
View Tutorial          By: Ajay at 2011-07-22 03:44:05

8. Good Thing yaar
Thank you

View Tutorial          By: Maninderjit Singh(Punjab) at 2011-08-02 05:41:45

9. if the simple example with syntex than very easy t
View Tutorial          By: Rikunj suthar at 2011-08-31 06:57:17

10. this is very helpful for me
View Tutorial          By: Amit Gaikwad at 2011-09-12 07:11:56

11. It is very Easy to understand.....
View Tutorial          By: premnath.s at 2011-09-13 09:01:05

12. Very easily understood...
View Tutorial          By: Keyur Panchal at 2011-09-23 05:04:20

13. i don't understand this thing
View Tutorial          By: ath at 2011-10-10 13:39:40

14. This is very useful for java beginners
View Tutorial          By: Syed Musthafa at 2011-11-20 07:11:50

15. This site has been helpful in explaining the basic
View Tutorial          By: Ayana Bagchi at 2011-12-04 17:24:39

16. better..................
implements........

View Tutorial          By: sunny at 2012-01-04 17:30:12

17. nice one
View Tutorial          By: dhusor at 2012-05-08 17:38:50

18. Excellent for beginners
View Tutorial          By: PRN at 2012-05-22 09:49:15

19. very nice to understand.... g
ood for begin

View Tutorial          By: Natarajan govindavel at 2012-06-07 03:57:27

20. Excellent example
View Tutorial          By: Dinesh Patil at 2012-06-19 08:30:48

21. very nice, ease to understand
View Tutorial          By: poorani at 2012-07-21 10:31:34

22. Hi Grenfel, would this be possible??


View Tutorial          By: Lerianne at 2012-08-20 11:37:32

23. Simply sooperb ty boss :-)
View Tutorial          By: Hosanna at 2012-08-31 22:38:47

24. thanks a lot fo the help offered.
View Tutorial          By: Douglas Olupotdolupot@cit.mak.ac.ug at 2012-10-31 05:37:04

25. thanks i understood
View Tutorial          By: raksha at 2012-11-04 16:44:19

26. the example is from herbert schildt complete ref..
View Tutorial          By: dui_prithibi at 2012-11-26 12:59:15

27. it's very helpful for java beginner. thanks
View Tutorial          By: Pisal at 2012-12-03 05:00:54

28. thanks
View Tutorial          By: snooker at 2013-02-04 19:45:40

29. Hi sir, Your explain is very good sir,
View Tutorial          By: Surendra Majji at 2013-02-14 04:55:49

30. Easy to understand.......
View Tutorial          By: Priya at 2013-02-15 10:55:52

31. very Good machi.......
View Tutorial          By: Janani at 2013-03-04 06:37:47

32. Thank you......
View Tutorial          By: Suvadip at 2013-04-19 05:11:51

33. Thank you! Nice article.
View Tutorial          By: Koja at 2013-04-23 20:58:05

34. vanistaks
View Tutorial          By: vanistaks at 2013-05-22 08:31:43

35. vanistaks
View Tutorial          By: vanistaks at 2013-05-27 11:41:53

36. vanistaks
View Tutorial          By: vanistaks at 2013-05-29 22:26:40

37. Isn't the Vector class deprecated now? I think Arr
View Tutorial          By: Brent at 2013-07-07 06:10:01

38. Can anybody tell.... what if condition is doing in
View Tutorial          By: Randhir Sambyal at 2013-08-26 06:37:25

39. you have helped me so much! thanks a lot ya! i rea
View Tutorial          By: NATION WHITE CHIRARA at 2013-09-19 07:53:57

40. HI Friends,

I am new to java and i

View Tutorial          By: Srini at 2015-04-05 17:00:58

41. @Srini

Hi Srini, I can answer your

View Tutorial          By: Sudev Wilson at 2015-04-08 12:23:33

42. chaala baag undi
View Tutorial          By: chaapundi at 2015-04-26 15:13:29

43. Copied from tutorialpoints.com
View Tutorial          By: nothing at 2016-02-10 05:01:36

44. kasieues
View Tutorial          By: kgsiedhu at 2017-03-17 14:39:38

45. aciadim
View Tutorial          By: aciadim at 2017-09-07 18:03:41