Vectors - PowerPoint PPT Presentation

Loading...

PPT – Vectors PowerPoint presentation | free to download - id: 1d94b-MDc4N



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Vectors

Description:

Rabbit bunny = new Rabbit(); You can do. vec.add(bunny); But you ... bunny = (Rabbit)vec.elementAt(0); Fixing the nuisance. class RabbitVector extends Vector ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 15
Provided by: davidleem
Learn more at: http://www.cis.upenn.edu
Category:
Tags: bunny | vectors

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Vectors


1
Vectors
2
Vectors and arrays
  • A Vector is like an array of Objects
  • Differences between arrays and Vectors
  • Arrays have special syntax Vectors dont
  • 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 dont need to know the size
    beforehand

3
Creating a Vector
  • import java.util.
  • Vector vec1 new Vector()
  • Vector vec2 new Vector(initialSize)
  • Vector vec3 new Vector(initialSize,
    increment)

4
Adding elements to a Vector
  • boolean add(Object o)
  • Appends the specified element to the end of this
    Vector. Always returns true.
  • void add(int index, Object element)
  • Inserts the specified element at the specified
    position in this Vector. The index must be 0
    and

5
Removing elements from a Vector
  • boolean remove(Object o)
  • Removes the first occurrence of the specified
    element from this Vector returns true if an
    element was removed
  • void remove(int index)
  • Removes the element at the specified position in
    this Vector
  • void removeAllElements()
  • Removes all elements

6
Accessing elements of a Vector
  • Object elementAt(int index) orObject get(int
    index)
  • Returns the component at the specified index
  • Object firstElement()
  • Returns the component at location 0
  • Object lastElement()
  • Returns the last component

7
Searching a Vector I
  • boolean contains(Object elem)
  • Tests if the specified object is a component of
    this Vector
  • int indexOf(Object elem)
  • Returns the index of the first occurrance of elem
    in this Vector, or -1 if not found
  • int indexOf(Object elem, int index)
  • Returns the index of the first occurrance of elem
    in this Vector, beginning the search at index or
    -1 if not found

8
Searching a Vector II
  • int lastIndexOf(Object elem)
  • Returns the index of the last occurrance of elem
    in this Vector, or -1 if not found
  • int lastIndexOf(Object elem, int index)
  • Returns the index of the last occurrance of elem
    in this Vector, searching backward from index or
    -1 if not found
  • All searching is done using equals

9
Getting information about a Vector
  • boolean isEmpty()
  • Tests if this Vector has no elements
  • int size()
  • Returns the number of elements in this Vector
  • Object toArray()
  • Returns an array containing all the elements of
    this Vector in the correct order

10
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
  • Its often a good idea to override equals for
    your own objects
  • The String class (and some others) override
    equals

11
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.elementAt(0)
  • Instead, you have to do
  • bunny (Rabbit)vec.elementAt(0)

12
Fixing the nuisance
  • class RabbitVector extends Vector Rabbit
    elementAt(int i) return
    (Rabbit)super.elementAt(i)
  • Now you can do
  • Vector vec new RabbitVector()
  • vec.add(bunny)
  • bunny vec.elementAt(0)

13
Conclusion
  • A Vector is like an array of Objects
  • The advantage of a Vector is that you dont need
    to know beforehand how big to make it
  • The disadvantage of a Vector is that you cant
    use the special syntax for arrays
  • You should never use an array that you hope is
    big enough--use a Vector instead

14
The End
About PowerShow.com