Thursday, September 23, 2010

List comparing to vector

Comparison to Vector

If you've used Vector(in the API reference documentation), you're already familiar with the general flavor of List. (Of course, List is an interface, while Vector is a concrete implementation.) List fixes several minor API deficiencies in Vector. For starters, commonly used Vector operations such as elementAt and setElementAt have been given much shorter names. When you consider that these two operations are the List analogue of square brackets for arrays, it becomes apparent that shorter names are highly desirable. Consider the following assignment statement:
a[i] = a[j].times(a[k]);
The Vector equivalent is:
v.setElementAt(v.elementAt(j).times(v.elementAt(k)), i);
The List equivalent is:
v.set(i, v.get(j).times(v.get(k)));
You may already have noticed that the set method, which replaces setElementAt, reverses the order of the arguments so that they match the corresponding array operation. Consider this assignment statement:
beatle[5] = "Billy Preston";
The Vector equivalent is:
beatle.setElementAt("Billy Preston", 5);
The List equivalent is:
beatle.set(5, "Billy Preston");
For consistency's sake, the add(int, Object) method, which replaces insertElementAt(Object, int), also reverses the order of the arguments. The various range operations in Vector (indexOf, lastIndexOf(setSize) have been replaced by a single range-view operation (subList), which is far more powerful and consistent.

No comments:

Post a Comment