Vector example in Java

By: Grenfel  

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.




Archived Comments

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

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

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

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

5. @Srini

Hi Srini, I can answer your query. Its a concept in java called autoboxing if

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

6. HI Friends,

I am new to java and i have one small doubt. In the above program we are

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

7. you have helped me so much! thanks a lot ya! i really loved it. now i can code a program using vecto
View Tutorial          By: NATION WHITE CHIRARA at 2013-09-19 07:53:57

8. Can anybody tell.... what if condition is doing in this program
???

View Tutorial          By: Randhir Sambyal at 2013-08-26 06:37:25

9. Isn't the Vector class deprecated now? I think ArrayList is the
preferred class, since it all

View Tutorial          By: Brent at 2013-07-07 06:10:01

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

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

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

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

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

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

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

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

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

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

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

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

22. thanks a lot fo the help offered.
View Tutorial          By: Douglas [email protected] at 2012-10-31 05:37:04

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

24. Hi Grenfel, would this be possible??

public void readFile() throws IOException{

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

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

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

27. very nice to understand.... g
ood for beginners

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

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

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

30. better..................
implements...........

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

31. This site has been helpful in explaining the basic understanding about Vector....
Kudos to th

View Tutorial          By: Ayana Bagchi at 2011-12-04 17:24:39

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

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

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

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

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

37. if the simple example with syntex than very easy to understand
it also better.

View Tutorial          By: Rikunj suthar at 2011-08-31 06:57:17

38. Good Thing yaar
Thank you

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

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

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

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

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

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

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

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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial