Vectors Vectors and arrays n n A Vector

  • Slides: 14
Download presentation
Vectors

Vectors

Vectors and arrays n n A Vector is like an array of Objects Differences

Vectors and arrays n n A Vector is like an array of Objects Differences between arrays and Vectors: n n n Arrays have special syntax; Vectors don’t You can have an array of any type, but a Vector holds Objects An array is a fixed size, but a Vector expands as you add things to it n This means you don’t need to know the size beforehand 2

Creating a Vector n import java. util. *; n Vector vec 1 = new

Creating a Vector n import java. util. *; n Vector vec 1 = new Vector(); n Vector vec 2 = new Vector(initial. Size); n Vector vec 3 = new Vector(initial. Size, increment); 3

Adding elements to a Vector n boolean add(Object obj) n Appends the object obj

Adding elements to a Vector n boolean add(Object obj) n Appends the object obj to the end of this Vector n Always returns true n n This is for consistency with other, similar classes void add(int index, Object element) n n Inserts the element at position index in this Vector The index must be greater than or equal to zero and less than or equal to the number of elements in the Vector 4

Removing elements from a Vector n n boolean remove(Object obj) n Removes the first

Removing elements from a Vector n n boolean remove(Object obj) n Removes the first occurrence of obj from this Vector n Returns true if an element was removed void remove(int index) n n Removes the element at position index from this Vector void remove. All. Elements() n Removes all elements 5

Accessing elements of a Vector n Object element. At(int index) or Object get(int index)

Accessing elements of a Vector n Object element. At(int index) or Object get(int index) n n n Object first. Element() n n Returns the component at position index element. At is an older method, retained for compatibility with older programs Returns the component at location 0 Object last. Element() n Returns the last component 6

Searching a Vector I n boolean contains(Object element) n n int index. Of(Object element)

Searching a Vector I n boolean contains(Object element) n n int index. Of(Object element) n n n Tests if element is a component of this Vector Returns the index of the first occurrence of element in this Vector Returns -1 if element was not found in this Vector int index. Of(Object element, int index) n n Returns the index of the first occurrence of element in this Vector, beginning the search at index Returns -1 if element was not found in this Vector 7

Searching a Vector II n int last. Index. Of(Object element) n n n int

Searching a Vector II n int last. Index. Of(Object element) n n n int last. Index. Of(Object element, int index) n n n Returns the index of the last occurrence of element in this Vector Returns -1 if element was not found in this Vector Returns the index of the last occurrence of element in this Vector, searching backward from index Returns -1 if element was not found in this Vector All searching is done using equals 8

Getting information about a Vector n boolean is. Empty() n n int size() n

Getting information about a Vector n boolean is. Empty() n n int size() n n Returns true if this Vector has no elements Returns the number of elements currently in this Vector Object[ ] to. Array() n Returns an array containing all the elements of this Vector in the correct order 9

More about equals n There are many different notions of equality n n Example:

More about equals n There are many different notions of equality n n Example: two sets are equal if they contain the same elements; order of elements is irrelevant Java defines public boolean equals(Object) in the Object class, but n n equals is defined to be the same as == It’s often a good idea to override equals for your own objects n n If you do this, note that the argument should be a general Object The String class (and some others) override equals 10

A minor nuisance n Suppose you define n n n You can do n

A minor nuisance n Suppose you define n n n You can do n n bunny = vec. get(0); Instead, you have to do n n vec. add(bunny); But you cannot do n n Vector vec = new Vector(); Rabbit bunny = new Rabbit(); bunny = (Rabbit)vec. get(0); Vectors are defined to hold Objects; when you get something out, Java doesn’t know what kind you expect it to be 11

Fixing the nuisance n n n You can extend Vector and override whatever methods

Fixing the nuisance n n n You can extend Vector and override whatever methods you choose class Rabbit. Vector extends Vector { Rabbit element. At(int i) { return (Rabbit)super. get(i); } } Now you can do n n n Vector vec = new Rabbit. Vector(); vec. add(bunny); bunny = vec. get(0); 12

Conclusion n n A Vector is like an array of Objects The advantage of

Conclusion n n A Vector is like an array of Objects The advantage of a Vector is that you don’t need to know beforehand how big to make it The disadvantage of a Vector is that you can’t use the special syntax for arrays You should never use an array that you hope is “big enough”—use a Vector instead 13

The End 14

The End 14