Vector

Threads
Chapter 10: Miscellaneous
Topics
Threads
1 - 11
Rev. 3.1
Threads
Table of Contents
Objectives ................................................................................................................3
Section 1: Introduction ..............................................................................................4
Executable JAR Files ..............................................................................................5
Vector ......................................................................................................................6
Generics...................................................................................................................8
Summary .............................................................................................................. 11
Threads
2 - 11
Rev. 3.1
Threads
Objectives
After completing this unit you will be able to:
Threads
3 - 11
Rev. 3.1
Threads
Section 1: Introduction
Threads
4 - 11
Rev. 3.1
Threads
Executable JAR Files
To create an executable JAR file:
Create a manifest file
Execute the jar command
The procedure described here is performed from the command window. To bring up the
command window select Start and then Run. IN the dialog box enter: cmd and then press the
return key. Next:
1) Create a manifest file using a tool such as Notepad. The file needs to contain the following
where className is the name of the class:
Main-Class: className
There should be a CRLF at the end of the line.
2) Next, create the JAR file using the following command:
C:>jar cmf manifestFile Application.jar ApplicationClassName.class
Where:
cmf – c means to create a new file
m means to modify the manifest
f means to output the archive to file
manifestFile - The name of the manifest file
Application.jar – The name of the JAR file to be created with the .jar extension
ApplicationClassName.class – The class(es) to be included on the JAR
The jar file can be executed using the command:
java –jar Application.jar
This command can be placed in a batch file to simplify its execution.
Threads
5 - 11
Rev. 3.1
Threads
Vector
The Vector class is found in the java.util package. It provides an implementation of a resizable
collection and implements the interfaces:
List interface
Collection
RandomAccess
Serializable
Cloneable
Iterable
The Vector class holds Objects. Generics, as discussed later, can ease the use of this and other
utility classes.
In the next example a vector is created where two integers and a String is added to it. Java boxes
and unboxes primitive data types as necessary. In this example the primitive integers are
converted to the Integer class
Vector vector = new Vector();
vector.add(12);
vector.add("alpha");
vector.add(8);
There are three common approaches to accessing the elements of the vector.
For loop
Iterator
For each loop
The for loop uses an integer index and the get method to return an element of the vector. When
an object is used in a println method the toString method of the object is executed and printed.
for(int i=0; i<vector.size();i++) {
System.out.println(vector.get(i));
}
The Iterator interface provides a hasNext and next method that can be used to iterate through the
vector. The iterator method of the Vector class returns an iterator.
Threads
6 - 11
Rev. 3.1
Threads
Iterator iterator = vector.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
The for each loop works on either arrays or collections.
for(Object element: vector) {
System.out.println(element);
}
If different types of objects are added to a Vector then it becomes necessary to determine the type
of object returned.
int total=0;
for(Object num: vector) {
if(num instanceof Integer){
total += (Integer)num;
}
}
System.out.println(total);
Threads
7 - 11
Rev. 3.1
Threads
Generics
Generics were introduced into Java with release 1.5. They provide an abstraction mechanism
that makes written certain type of code easier and safer. For example, if you wanted to iterate
over a Vector of integers you could use code similar to the following:
Vector v = new Vector();
v.add(1);
v.add(2);
v.add(3);
Iterator iterator = v.iterator();
while(iterator.hasNext()) {
int temp = iterator.next();
// Do something with temp
}
However, the statement:
int temp = iterator.next();
Generates the error:
Type mismatch: cannot convert from Object to int
To avoid the problem a cast is required.
int temp = (Integer)iterator.next();
This is annoying at best.
Generics allows us to declare the vector to be of a specific type. This makes it easier to use and
avoids certain types of errors.
Notice that the first line generates a warning:
Threads
8 - 11
Rev. 3.1
Threads
Vector is a raw type. References to generic type Vector<E>
should be parameterized
Replacing this line with:
Vector<Integer> v = new Vector<Integer>();
Eliminates the error and declares that the vector is suppose to hold integers and nothing but
integers. If something other than an integer is added:
v.add(4.5);
An error is generated:
The method add(Integer) in the type Vector<Integer> is not applicable for the arguments (double)
Also it avoids having to use the cast operator unnecessarily. Notice that the Iterator is also
declared using generics:
Iterator<Integer> iterator = v.iterator();
while(iterator.hasNext()) {
int temp = iterator.next();
// Do something with temp
}
An improved version follows:
Vector<Integer> v = new Vector<Integer>();
v.add(1);
v.add(2);
v.add(3);
Iterator<Integer> iterator = v.iterator();
Threads
9 - 11
Rev. 3.1
Threads
while(iterator.hasNext()) {
int temp = iterator.next();
// Do something with temp
}
See http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf for more detail.
Threads
10 - 11
Rev. 3.1
Threads
Summary
Threads
11 - 11
Rev. 3.1