Experiments in Java


Notes on Session X2: Vectors

Vectors. Many computer scientists object to the use of the term vector to describe the dynamic data structure which resembles arrays. There are a number of different objections, including the lack of standardization of the name (different languages and different researchers attach different meaning to ``vector'' as a data structure); the mismatch between the data type and the traditional mathematical or physical meaning of the term; and even the particular's of Java's implementation.

Experiment X2.1, Step 2. As you may recall, floating point numbers are often approximate representations. Hence, when we convert from Integer.MAX_VALUE-3 (2147483644) to float and back again, Java approximates the result as Integer.MAX_VALUE.

Experiment X2.1, Step 3. As the problem says, there is no particularly good way to keep track of the type once you've stored the value in the array, because Java does not keep track of the value. One possibility would be to create a second array and store an indication of type in the second array of booleans, isFloat. That is, if the value stored in a place in stuff is an integer, you might store false in the corresponding place in isFloat; if the value stored in the first array is a float, you might store true in the corresponding place in isFloat. For example,

    stuff[0] = (float) 3.5;   isFloat[0] = true;
    stuff[1] = i;             isFloat[1] = false;

Of course, this does not do anything about the lack of accuracy.

Experiment X2.1, Step 7. While Java permits you to assign integers to floats because it knows how to coerce integers, it does not permit you to assign arbitrary objects to floats. Your compiler should have given you a message to that effect.

Experiment X2.2, Step 4. As this example suggests, the java.util.Vector class provides a mechanism for building string representations of vectors, which makes it possible (one might even say easy) to print vectors. In fact, most classes provide a toString method for this purpose. Can you find a way to print arrays?

Experiment X2.2, Step 8. Since the whole vector contains the same elements in the same order, except for the deleted element, and the previous element 2 is now element 1, it is clear that the remaining elements shifted in the vector.

Experiment X2.3, Step 3. You are only allowed to set an element of a vector if you've already placed an element there (typically, using addElement).

Experiment X2.3, Step 5. Deleting an element shrinks the size of the vector. Hence, although there was previously an element with index 2, after the deletion there is not, so the replacement is illegal.

Experiment X2.3, Step 6. Here is one possible set of instructions, designed to work in a variety of cases.

    while (vec.size() < vec.capacity()) {
      vec.addElement("Added");
      out.println("Size " + vec.size());
      out.println("Capacity: " + vec.capacity());
    }

Experiment X2.3, Step 11. Although vectors expand when you add too many elements, they do not shrink again when you remove elements from them (or at least they don't shrink in typical cases).

Experiment X2.3, Step 12. Just as it is illegal to add elements beyond the end of the vector, it is also illegal to delete them. Remember that the last valid index is one less than the size of the vector!

Experiment X2.5, Step 4. Since ``Sam Rebelsky'' is already in the class, adding him again should make no difference.

Experiment X2.5, Step 6. We've deleted a student, so that student no longer appears in the class.

Experiment X2.5, Step 7. In order to look for a student, we need a Student object. Since we don't have an existing one, we must create a new one. Fortunately, the Student class's equals method does not try to ensure that two Students are precisely the same object; rather, it just verifies that they have the same name.

Experiment X2.5, Step 9. While we've set the grade of samr, a separate Student object with name ``Sam Rebelsky'' is stored in cs1. Changes to one do not affect the other.

Experiment X2.5, Step 10. The reasoning is similar to the previous problem. While we've used samr to find the student whose grade to set, we've set the grade of a separate Student object with name ``Sam Rebelsky'' is stored in cs1. Changes to one do not affect the other.

Experiment X2.6, Step 4. The designers of Java wanted programmers to be particularly careful when assigning and using objects. In particular, when using a general Object as a particular type of object, you must cast it to that type of object, even if it is obvious what type the object has.

Experiment X2.6, Step 5. This is the same issue as the previous problem. Because the code does not explicitly cast elementAt(i) to a string, Java will not treat it as a string, and therefore won't support the toUpperCase method. The line should read

        vec.setElementAt(((String) vec.elementAt(i)).toUpperCase(), i);

It is a matter of personal programming style whether you prefer (and use) this more concise code or the multi-line version.

Experiment X2.6, Step 7. Note that firstOfMonth does not first check to ensure that the vector elements are dates before casting them. When the program attempts to cast a string to a date, Java reports an error. Since it is not possible to tell the type of an object until the program executes, the Java compiler does not flag this as an error, but the runtime system does.


Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Tue Oct 26 12:09:56 1999.

This page generated on Tue Oct 26 15:36:09 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu