Vectors Vectors and arrays A Vector is like

  • Slides: 14
Download presentation
Vectors

Vectors

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

Vectors and arrays • A Vector is like an array of Objects • Differences between arrays and Vectors: – 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 • This means you don’t need to know the size beforehand

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

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

Adding elements to a Vector • boolean add(Object o) – Appends the object o

Adding elements to a Vector • boolean add(Object o) – Appends the object o to the end of this Vector – Always returns true • This is for consistency with other, similar classes • void add(int index, Object element) – 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

Removing elements from a Vector • boolean remove(Object o) – Removes the first occurrence

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

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

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

Searching a Vector I • boolean contains(Object elem) – Tests if elem is a

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

Searching a Vector II • int last. Index. Of(Object elem) – Returns the index

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

Getting information about a Vector • boolean is. Empty() – Returns true if this

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

More about equals • There are many different notions of equality – Example: two

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

A minor nuisance • Suppose you define Vector vec = new Vector(); Rabbit bunny

A minor nuisance • Suppose you define Vector vec = new Vector(); Rabbit bunny = new Rabbit(); • You can do vec. add(bunny); • But you cannot do bunny = vec. element. At(0); • Instead, you have to do bunny = (Rabbit)vec. element. At(0);

Fixing the nuisance class Rabbit. Vector extends Vector { Rabbit element. At(int i) {

Fixing the nuisance class Rabbit. Vector extends Vector { Rabbit element. At(int i) { return (Rabbit)super. element. At(i); } } • Now you can do Vector vec = new Rabbit. Vector(); vec. add(bunny); bunny = vec. element. At(0);

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

Conclusion • 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

The End

The End