DOC PREVIEW
Penn CIT 591 - Vectors

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 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 beforehand3Creating a Vectorimport java.util.*;Vector vec1 = new Vector();Vector vec2 = new Vector(initialSize);Vector vec3 = new Vector(initialSize, increment);4Adding elements to a Vectorboolean add(Object obj)Appends the object obj 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 Vector5Removing elements from a Vectorboolean remove(Object obj)Removes the first occurrence of obj from this VectorReturns true if an element was removedvoid remove(int index)Removes the element at position index from this Vectorvoid removeAllElements()Removes all elements6Accessing elements of a VectorObject elementAt(int index) orObject get(int index)Returns the component at position indexelementAt is an older method, retained for compatibility with older programsObject firstElement()Returns the component at location 0Object lastElement()Returns the last component7Searching a Vector Iboolean contains(Object element)Tests if element is a component of this Vectorint indexOf(Object element)Returns the index of the first occurrence of element in this VectorReturns -1 if element was not found in this Vectorint indexOf(Object element, int index)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 Vector8Searching a Vector IIint lastIndexOf(Object element)Returns the index of the last occurrence of element in this VectorReturns -1 if element was not found in this Vectorint lastIndexOf(Object element, int index)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 equals9Getting information about a Vectorboolean isEmpty()Returns true if this Vector has no elementsint size()Returns the number of elements currently in this VectorObject[ ] toArray()Returns an array containing all the elements of this Vector in the correct order10More 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 public 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If you do this, note that the argument should be a general ObjectThe String class (and some others) override equals11A 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.get(0);Instead, you have to do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 be12Fixing the nuisanceYou can extend Vector and override whatever methods you chooseclass RabbitVector extends Vector { Rabbit elementAt(int i) { return (Rabbit)super.get(i); }}Now you can doVector vec = new RabbitVector();vec.add(bunny);bunny = vec.get(0);13Conclusion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 instead14The


View Full Document

Penn CIT 591 - Vectors

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Vectors
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Vectors and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Vectors 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?