| 95.105 - Introduction to Programming |
Fall 2001
|
7 Vectors, Enumerations and StringTokenizers |
| 7.1 Vectors and ArrayLists |
A Vector (or ArrayList):
| Vector myVector = new
Vector();
//initial capacity size 10
Vector myVector = new Vector(int cap); //initial capacity size cap Vector myVector = new Vector(Collection col); //make a new Vector initially with the elements //being the same as those in the given collection, //which may be another Vector |
Note that the ArrayList has similar constructors:
| ArrayList myList = new
ArrayList();
ArrayList myList = new ArrayList(int cap); ArrayList myList = new ArrayList(Collection col); |
Here are some of the standard Vector methods. Note that
the ArrayList uses the same methods as the Vector. Previous
versions of java used different methods for the Vector class.
You should use the most recent versions shown here.
|
|
|
|
| void | add(Object obj) | Place the given object at the end of the Vector. If there is no room, it grows by its increment. |
| void | add(int pos, Object obj) | Insert the given object at the given position in the Vector. All other elements are shifted upward to have an index one greater that they had previously. |
| boolean | addAll(Collection col) | Adds all the elements of the given collection (which may be another Vector) to the receiver Vector. Returns true if the receiver Vector changed as a result of this call. |
| Object | get(int pos) | Return the element at the given position in the Vector. |
| void | set(int pos, Object obj) | Replace the element at the given position with the given object. |
| boolean | remove(Object obj) | Remove the first occurence of the given object from the vector. It returns true if it found the object, otherwise false. All elements after it are shifted toward the beginning of the vector. |
| Object | remove(int pos) | Remove the element at the given position in the vector. The newer version returns the object that was removed, the older version returns void. |
| boolean | removeAll(Collection col) | Removes from the receiver vector all elements that are in the given collection. Return true if the receiver changed. |
| boolean | retainAll(Collection col) | Removes from the receiver vector all elements that are NOT in the given collection. Return true if the receiver changed. |
| void | clear() | Remove all the elements from the vector. |
| boolean | isEmpty() | Return whether or not there are any elements in the vector. |
| boolean | contains(Object obj) | Return whether or not the vector contains the given object. The object method equals() is used to determine this. |
| boolean | containsAll(Collection col) | Return whether or not the vector contains all the objects that are within the given collection. The object method equals() is used to determine this. |
| int | indexOf(Object obj) | Return the position of the first occurrence of the given object. -1 is returned if the object is not there. |
| int | size() | Return the number of elements in the vector. |
| int | capacity() | Return the current capacity of the vector. |
| Enumeration | elements() | Return an enumeration containing the elements of the vector. This does not work for ArrayLists, only Vectors. |
| Iterator | iterator() | Return an iterator containing the elements of the vector. |
Note that the get() method has a return type of Object. That means whenever you extract an object from a Vector, you must then type cast it to the kind of object that you want. In general, a Vector may contain multiple types of objects, so a Vector must supply methods that support the extraction of any types of objects as well. Now since a method can only have one return type, Java would need to make a special method for each kind of object that may be possibly stored in a Vector. Clearly this is impossible and so Java decided to have a return type of Object, since all objects are subclasses of Object anyway. So, upon getting an element out of the Vector, we must type cast it back into its original type (or as we'll see later to a super type or interface) so that we can use the object again.
Here is how to use a Vector to store some integers, perhaps peoples ages. Note that we must use the Integer wrapper class:
Vector v = new Vector();Notice that we had to type-cast the object coming out of the vector. Also, we used intValue() to unwrap the Integer so that we could use it as a data type in a calculation. As we can see, it is a little tedious to have to wrap up the data types like this, but that's the way it MUST be done.
v.add(new Integer(4));
v.add(new Integer(23));
v.add(new Integer(76));
v.add(new Integer(34));
v.add(new Integer(28));//System.out.println(v.contains(76)); //This won't compile
System.out.println(v.contains(new Integer(76)));double sum = 0;
for (int i=0; i<v.size();i++) {
int value = ((Integer)v.get(i)).intValue();
sum += value;
}
System.out.println(sum/v.size());
Lets take a look at another example that stores Pet objects (assume that Pets have names). This time we'll use ArrayLists:
ArrayList v = new ArrayList();Now if we wanted to get those pets back out and print out their names, we must use type-casting again:
String[] names = {"Al", "Zack", "Sally", "Mel", "Fry", "Jorg", "Jake", "Sven", "Ren", "Stimpy", "Jim", "Orson", "Ash" };
for (int i=0; i<names.length;i++) {
Pet p = new Pet();
p.setName(names[i]);
v.add(p);
}
for (int i=0; i<v.size();i++) {Notice that the ArrayLists are used the same as the Vector class.
Pet p = (Pet)v.get(i);
System.out.println(p.getName());
}
| 7.2 Enumerations and Iterators |
There are two useful methods defined by the Enumeration interface:
nextElement();
//accesses and returns the next element in the
//enumeration. An error occurs if there are no more.
Here is an example of how to use the Enumeration class on a Vector:
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tuesday");
dayNames.add("Wednesday");
dayNames.add("Thursday");
dayNames.add("Friday");
dayNames.add("Saturday"};
days = dayNames.elements();
while (days.hasMoreElements())
| Sunday
Monday Tuesday Wednesday Thursday Friday Saturday |
Enumerations do have problems though. If for example we want to enumerate through some items in a vector and remove items as well from the vector, then this does not work properly. For some reason, Java does not allow you to remove from a collection that you are enumerating through !! Consider this (logically correct) example, and then look at the output to follow:
aVector.add("one");
aVector.add("two");
aVector.add("three");
aVector.add("four");
aVector.add("five");
aVector.add("six");
aVector.add("seven");
aVector.add("eight");
aVector.add("nine");
aVector.add("ten");
System.out.println("Before Vector: "
+ aVector);
nums = aVector.elements();
while(nums.hasMoreElements())
{
Here is the output:
| Before Vector: [one, two, three, four, five,
six, seven, eight, nine, ten]
one two three five six seven nine ten After Vector: [one, two, four, five, six, eight, nine, ten] |
Notice that the Enumeration did not produce all the items. Also, the vector after removing is not correct since the "eight" string was NOT removed as it should have been. This is a problem with Enumerations.
The moral is...don't use Enunerations if you are gonna be removing from the collection that you are enumerating through!
Instead, use an Iterator. An interator is a tool for iterating (i.e., traversing) through the elements of a collection. To get an iterator, send the iterator() message to a collection (such as a Vector):
aVector.iterator();
An iterator works a lot like an enumeration and has the following methods
available:
| boolean | hasNext() | returns whether or not there is another element in the collection |
| Object | next() | returns the next element in the collection |
| void | remove() | removes the item from the collection that was just obtained from a call to next(). Note that you don't specify here the item to be removed. Also, note that the item is actually removed from the collection! |
Here is how to use the iterator in a similar example to that above:
aVector.add("one");
aVector.add("two");
aVector.add("three");
aVector.add("four");
aVector.add("five");
aVector.add("six");
aVector.add("seven");
aVector.add("eight");
aVector.add("nine");
aVector.add("ten");
System.out.println("Before Vector: "
+ aVector);
nums = aVector.iterator();
while(nums.hasNext())
{
Here is the resulting output:
| Before Vector: [one, two, three, four, five,
six, seven, eight, nine, ten]
one two three four five six seven eight nine ten After Vector: [one, two, four, five, six, nine, ten] |
Notice that we are now saying remove() to the iterator, NOT to the Vector. This is important. The iterator essentially keeps itself coordinated with the collection that it is iterating through so that no elements are lost along the way.
| 7.3 The Car and Autoshow Example |
Consider an Autoshow that contains various Cars from various companies. Customers go to the autoshow with dreams of someday owning cars such as these and/or to investigate the cars/trucks/minivans that are accessable in their price range. We will pick four characteristics of the cars that are interesting. Obviously when buying a car, there are many other options and these can easily be added. For each car, we will keep the name, make, color and topSpeed. The first step in making the object is to define the Car class and create its get and set methods, its constructor and a toString() method.
//Here
are the get and set methods
public String
getName() { return(name); }
public String
getColor() { return(color); }
public String
getMake() { return(make); }
public int
getTopSpeed() { return(topSpeed); }
public void setName(String
aName) { name = aName; }
public void setColor(String
aColor) { color = aColor; }
public void setMake(String
aMake) { make = aMake; }
public void setTopSpeed(int
aSpeed) { topSpeed = aSpeed; }
//Here
is the constructor
public Car(String
aName, String aMake, String aColor, int aTopSpeed) {
//This
method returns a string representing the car
public String
toString() {
| Here
are some cars:
(Red Porsche 959) (Red Pontiac GrandAm GT) (White Ford Mustang) (Blue Volkswagon Rabbit) (Silver Volkswagon Jetta) |
An Autoshow should maintain a vector of Car objects. Again, we'll make appropriate get, constructor and toString methods. Note that the constructor will set the cars to be a new vector. We do not ever need to set the cars instance variable since it will always be this vector. We will merely add to the vector when cars arrive at the autoshow. Note that we must import the Vector class from the utilities package. We'll also import the Enumeration class as we will need this later.
//Here
is the get method
public Vector
getCars() { return(cars); }
//Here
is the constructor
public Autoshow()
{
//This
method returns a string representing the car
public String
toString() {
//This
method returns the number of cars in the autoshow
public int carCount()
{
someCars = cars.elements();
while(someCars.hasMoreElements())
{
someCars = cars.elements();
while(someCars.hasMoreElements())
{
if (cars.size() == 0)
//First
add lotsa cars to the show
anAutoshow.addCar(new
Car("959", "Porsche",
"red",
340));
anAutoshow.addCar(new
Car("Grand-Am", "Pontiac",
"White",
160));
anAutoshow.addCar(new
Car("Mustang", "Ford",
"White",
230));
anAutoshow.addCar(new
Car("Rabbit", "Volkswagon",
"Blue",
100));
anAutoshow.addCar(new
Car("Jetta", "Volkswagon",
"Silver",
130));
anAutoshow.addCar(new
Car("Storm", "Geo",
"Yellow",
140));
anAutoshow.addCar(new
Car("MR2", "Toyota",
"Black",
210));
anAutoshow.addCar(new
Car("Escort", "Ford",
"Yellow",
10));
anAutoshow.addCar(new
Car("Civic", "Honda",
"Black",
180));
anAutoshow.addCar(new
Car("Altima", "Nissan",
"Silver",
190));
anAutoshow.addCar(new
Car("525", "BMW",
"Gold",
490));
anAutoshow.addCar(new
Car("Prelude", "Honda",
"White",
90));
anAutoshow.addCar(new
Car("RX7", "Mazda",
"Red",
220));
anAutoshow.addCar(new
Car("MX6", "Mazda",
"Green",
180));
anAutoshow.addCar(new
Car("Firebird", "Pontiac",
"Black",
140));
//Now
test our fun methods
System.out.println(anAutoshow);
System.out.println("Here
are the Pontiac cars:");
System.out.println(anAutoshow.carsWithMake("Pontiac"));
System.out.println("Here
are the Ford cars:");
System.out.println(anAutoshow.carsWithMake("Ford"));
System.out.println("Here
are the different makes:");
System.out.println(anAutoshow.differentMakes());
System.out.println("This
is the fastest car:");
System.out.println(anAutoshow.fastestCar());
System.out.println("The
most common color is " +
anAutoshow.mostCommonColor());
System.out.println("\nHere
are the cars sorted by top speed:");
anAutoshow.printBySpeed();
System.out.println("\nHere
are the cars sorted by make:");
anAutoshow.printByMake();
}
| Autoshow
with 15 cars
Here are the Pontiac cars: [(White Pontiac Grand-Am), (Black Pontiac Firebird)] Here are the Ford cars: [(White Ford Mustang), (Yellow Ford Escort)] Here are the different makes: [Porsche, Pontiac, Ford, Volkswagon, Geo, Toyota, Honda, Nissan, BMW, Mazda] This is the fastest car: (Gold BMW 525) The most common color is White Here
are the cars sorted by top speed:
Here
are the cars sorted by make:
|
| 7.4 String Tokenizer |
We ca create a StringTokenizer as follows:
| StringTokenizer tokens = new StringTokenizer(String aString); |
The default deliminators are " \n\t\r". That is, the string tokens
are separated by spaces, newlines, tabs and carriage returns.
It is used similar to Enumerations. Here are the useful methods:
|
|
|
|
| int | countTokens() | Returns the number of tokens of the String to be tokenized. |
| boolean | hasMoreTokens() | Returns whether or not there are more tokens in the String being tokenized. |
| String | nextToken() | Return the next token in the String being tokenized. |
Example
Here is a big example that tests many of the String methods:
System.out.println("The
String has " + words.countTokens() + " words."
);
System.out.println("Here
are the words, one by one:");
while(words.hasMoreTokens())
| The
String has 5 words.
Here are the words, one by one: Half of 12 is six |
Use a StringTokenizer whenever you find that you need to break apart a String into pieces.