Thursday, May 19, 2011

Ensuring use of the new Vector API methods

When the new Collections API was introduced in Java 2 to provide uniform data structure classes, the Vector class was updated to implement the List interface. Use the List methods because they are common to other data structure. If you later decide to use something other than a Vector (eg, ArrayList, or LinkedList, your other code will not need to change.
Even up thru the first several versions of Java 2 (SDK 1.4), the language had not entirely changed to use the new Collections methods. For example, the DefaultListModel still uses the old methods, so if you are using a JList, you will need to use the old method names. There are hints that they plan to change this, but still and interesting omission.

When you create a Vector, you can assign it to a List (a Collections interface). This will guarantee that only the List methods are called.

Vector v1 = new Vector();  // allows old or new methods.
List v2 = new Vector(); // allows only the new (List) methods.

See here for replacements of old methods in Vector API.

No comments:

Post a Comment

Chitika