![]() |
Arrays vs Vectors |
|||||||||||
| Array a | Vector v | |
| Element type | declaration of a, e.g., int a[10], so each element is an int | reference to Object (must cast) |
| Element referenced by | a[index] | (cast)v.elementAt(index) TYPOS IN BOOK |
| Element assignment by | a[index] = ... | v.setElementAt (Object obj, index) TYPO IN BOOK |
| Number of elements | a.length | v.size() |
| Things it can store | primitive and classes (objects) | classes (objects) |
| Dynamic | NO | YES |
| Defined by | Java language | Java class library |
boolean search(String s) { // Returns true if and only if s equals one
// of the Strings in v.
int k; // k== the index of the next position in
// the array to check.
// No match has been found in positions
// 0 through k-1.
k = 0;
while (k!=v.length && !s.equals(v[k]))
k++;
// k==v.length || s.equals(v[k])
return k!=v.length;
}