February 17, 2019
Hot Topics:

# Objects and Collections: Vectors

First, you can determine the size and capacity of the Vector with the code added in Listing 7.

```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);
}

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

System.out.println("nsize: "+myVec.size());
System.out.println("capacity: "+myVec.capacity());

}
}
```

Listing 7: TestVector.java

When this code is run, you can see that the size and the capacity are both 5, just as you expected.

```C:column25>"C:Program FilesJavajdk1.5.0_06binjava"
-classpath . TestVector

myEnum: 0
myEnum: 1
myEnum: 2
myEnum: 3
myEnum: 4

size: 5
capacity: 5
C:column25>
```

Now, prove that you actually can "grow" the Vector. Listing 8 includes code to add two new elements to the 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);
}

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

System.out.println("nsize: "+myVec.size());
System.out.println("capacity: "+myVec.capacity());

myInt = new Integer(6);

System.out.println("nsize: "+myVec.size());
System.out.println("capacity: "+myVec.capacity());

myInt = new Integer(7);

System.out.println("nsize: "+myVec.size());
System.out.println("capacity: "+myVec.capacity());

}
}
```

Listing 8: TestVector.java

When this code is executed, you can see that the Vector has grown in size as you expected.

```C:column25>"C:ProgramvFilesJavajdk1.5.0_06binjava"
-classpath . TestVector

myEnum: 0
myEnum: 1
myEnum: 2
myEnum: 3
myEnum: 4

size: 5
capacity: 5

size: 6
capacity: 10

size: 7
capacity: 10
C:column25>
```

Note that the size of the Vector has increased by one as each element was added. The interesting thing to see here is that when the 6th element was added, the capacity increased to 10, not 6. Then when the 7th element was added, the capacity remained at 10. The Java documentation explains this feature as follows:

"Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation."

Regardless of how the Vector is implemented internally, you have demonstrated that it does indeed "grow."

#### Compiler Issues & Information Gathering

You may have noticed that when you compile the code examples above, you get the following warning. (You will explore these issues in great detail when you explore the changes that have evolved in the Vector class specifically, and the Collection framework in general.)

```C:column25>"C:Program FilesJavajdk1.5.0_06binjavac"
-classpath . TestVector.java
Note: TestVector.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:column25>
```

Although the compiler calls them Notes, the messages are basically warnings (and you should always pay attention to warnings provided by the compiler). The line between warnings and outright errors has blurred a bit over the years as new versions of the JDK/SDK have evolved. Regardless of whether they are warnings or errors, you should focus on the first note: TestVector.java uses unchecked or unsafe operations. The second note provides a clue about how you can get more information.

By using the Xlint option in the compiler's parameter list, you can get a more detailed explanation as to why the warning was provided.

```"C:Program FilesJavajdk1.5.0_06binjavac" -Xlint
-classpath . TestVector.java
```

When you add this option, you get the following output (notice that the compiler now identifies the note as a warning):

```C:column25>"C:Program FilesJavajdk1.5.0_06binjavac"
-Xlint -classpath . TestVector.java
TestVector.java:9: warning: [unchecked] unchecked call to addElement(E)
as a member of the raw type java.util.Vector
^
1 warning
C:column25>
```

Be aware that this code actually does compile successfully; however, you need to address the warning to make this code safe to implement, or at least compliant with the SDK 1.5.

I will cover this topic in next month's column.

### References

• www.sun.com
• Just Java 2, 6th Edition. Peter van der Linden. 2004, Sun Microsystems.

Matt Weisfeld is a faculty member at Cuyahoga Community College (Tri-C) in Cleveland, Ohio. Matt is a member of the Information Technology department, teaching programming languages such as C++, Java, C#, and .NET as well as various Web technologies. Prior to joining Tri-C, Matt spent 20 years in the information technology industry gaining experience in software development, project management, business development, corporate training, and part-time teaching. Matt holds an MS in computer science and an MBA in project management. Besides The Object-Oriented Thought Process, which is now in its second edition, Matt has published two other computer books, and more than a dozen articles in magazines and journals such as Dr. Dobb's Journal, The C/C++ Users Journal, Software Development Magazine, Java Report, and the international journal Project Management. Matt has presented at conferences throughout the United States and Canada.

Page 3 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.

## Most Popular Developer Stories

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