April 22, 2019
Hot Topics:

Objects and Collections: Vectors

  • August 3, 2006
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Thinking in Terms of Objects

The syntax used to operate on an array makes it look like an Array is simply an extension of a primitive variable, and in some ways it really appears that way.

For example, you can assign an integer like this:

x = 5;

Likewise, you can assign an array with a similar syntax.

items[1] = 5;

The point of interest here is that this really is not object-oriented at all. You should be able to provide the reason why just by looking at the code in the array example. There are actually several reasons why the code items[1] = 5; breaks the object-oriented paradigm; however, perhaps the most obvious is that the fundamental concept of encapsulation is broken. The quick way to identify this problem is that assignments must be made via methods. In the code above, the assignment is performed directly to the array item, not via a method.

Note: See my article, Exploring Encapsulation, for a detailed explanation of encapsulation.

Based on the suspicion pertaining to the direct assignment of the array item, you may have guessed that the way to assign something to a Vector will require something a bit more object-oriented—like the use of a method. Take a look at the Vector's addElement( ) method.

Method Summary

boolean addElement(Object obj) Adds the specified component to the end of this vector, increasing its size by one.

Althugh the use of a method is a safe way to add an item to a Vector, it is also interesting to look at what is passed by the method, an object. This has an interesting implication; you can't assign primitives to a Vector because only an Object can be passed as a parameter. So, you can't do something like this:

import java.io.*;
import java.util.*;

public class TestVector {
   public static void main(String[] args){
      Vector myVec = new Vector();
      int x = 5;

Listing 4: TestVector.java

Here is where the primitive wrappers come in play. If the Vector does not like to accept raw primitives, you need to send it an Object. However, if you really want a Vector of integers, you have to wrap the integers in Integer objects. Look at the code in Listing 5.

import java.io.*;
import java.util.*;

public class TestVector {
   public static void main(String[] args){

      Vector myVec = new Vector(5);
      Integer myInt = null;

      myInt = new Integer(1);

Listing 5: TestVector.java

The code in Listing 5 successfully adds an element to the Vector. In this case, the element is an Integer object. However, getting the element into the Vector is only part of the story. You also need to get the element(s) out of the Vector. And, getting things out of a Vector is different than getting things out of an array.

The typical method for processing an array involves using loops like the code fragment listed below.

for (int j = 0; j<5; j++) {



This is not the case for a Vector. The original method for processing a Vector involved using loops along with Enumerations.

Listing 6 displays a program that uses an Enumeration to process a Vector.

import java.io.*;
import java.util.*;

public class TestVector {
   public static void main(String[] args){
      Vector myVec = new Vector(5);
      Integer myInt = null;

      for(int i=0;i<5;i++){
         myInt = new Integer(i);
         System.out.println("addElement: " + myInt);

      Enumeration myEnum = myVec.elements();
         System.out.println("myEnum: "+(Integer)myEnum.nextElement());


Listing 6: TestVector.java

The code to process the Vector involves loading the Enumeration with the Vector and then processing the Enumeration as long as there are elements left in the Enumeration using the hasMoreElements() method.

Enumeration myEnum = myVec.elements();    // load the Vector into myEnum
while (myEnum.hasMoreElements())          // loop thru myEnum
   System.out.println("myEnum: "+ (Integer) myEnum.nextElement());

When this code is executed, you get the following output.

C:column25>"C:Program FilesJavajdk1.5.0_06binjava"
   -classpath . TestVector
addElement: 0
addElement: 1
addElement: 2
addElement: 3
addElement: 4
myEnum: 0
myEnum: 1
myEnum: 2
myEnum: 3
myEnum: 4

So, you can see that the Vector was successfully loaded and later accessed correctly. Now, see how you now can "grow" the Vector.

Page 2 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date