VectorsVectors and arraysCreating a VectorAdding elements to a VectorRemoving elements from a VectorAccessing elements of a VectorSearching a Vector ISearching a Vector IIGetting information about a VectorMore about equalsA minor nuisanceFixing the nuisanceConclusionThe EndJan 14, 2019Vectors2Vectors and arraysA Vector is like an array of ObjectsDifferences between arrays and Vectors:Arrays have special syntax; Vectors don’tYou can have an array of any type, but a Vector holds ObjectsAn array is a fixed size, but a Vector expands as you add things to itThis means you don’t need to know the size beforehand3Creating a Vectorimport java.util.*;Vector vec1 = new Vector();Vector vec2 = new Vector(initialSize);Vector vec3 = new Vector(initialSize, increment);4Adding elements to a Vectorboolean add(Object obj)Appends the object obj to the end of this VectorAlways returns trueThis is for consistency with other, similar classesvoid add(int index, Object element)Inserts the element at position index in this VectorThe index must be greater than or equal to zero and less than or equal to the number of elements in the Vector5Removing elements from a Vectorboolean remove(Object obj)Removes the first occurrence of obj from this VectorReturns true if an element was removedvoid remove(int index)Removes the element at position index from this Vectorvoid removeAllElements()Removes all elements6Accessing elements of a VectorObject elementAt(int index) orObject get(int index)Returns the component at position indexelementAt is an older method, retained for compatibility with older programsObject firstElement()Returns the component at location 0Object lastElement()Returns the last component7Searching a Vector Iboolean contains(Object element)Tests if element is a component of this Vectorint indexOf(Object element)Returns the index of the first occurrence of element in this VectorReturns -1 if element was not found in this Vectorint indexOf(Object element, int index)Returns the index of the first occurrence of element in this Vector, beginning the search at indexReturns -1 if element was not found in this Vector8Searching a Vector IIint lastIndexOf(Object element)Returns the index of the last occurrence of element in this VectorReturns -1 if element was not found in this Vectorint lastIndexOf(Object element, int index)Returns the index of the last occurrence of element in this Vector, searching backward from indexReturns -1 if element was not found in this VectorAll searching is done using equals9Getting information about a Vectorboolean isEmpty()Returns true if this Vector has no elementsint size()Returns the number of elements currently in this VectorObject[ ] toArray()Returns an array containing all the elements of this Vector in the correct order10More about equalsThere are many different notions of equalityExample: two sets are equal if they contain the same elements; order of elements is irrelevantJava defines public boolean equals(Object) in the Object class, butequals is defined to be the same as ==It’s often a good idea to override equals for your own objectsIf you do this, note that the argument should be a general ObjectThe String class (and some others) override equals11A minor nuisanceSuppose you defineVector vec = new Vector();Rabbit bunny = new Rabbit();You can dovec.add(bunny);But you cannot dobunny = vec.get(0);Instead, you have to dobunny = (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 be12Fixing the nuisanceYou can extend Vector and override whatever methods you chooseclass RabbitVector extends Vector { Rabbit elementAt(int i) { return (Rabbit)super.get(i); }}Now you can doVector vec = new RabbitVector();vec.add(bunny);bunny = vec.get(0);13ConclusionA Vector is like an array of ObjectsThe advantage of a Vector is that you don’t need to know beforehand how big to make itThe disadvantage of a Vector is that you can’t use the special syntax for arraysYou should never use an array that you hope is “big enough”--use a Vector instead14The
View Full Document