The Vector Class - PowerPoint PPT Presentation

About This Presentation
Title:

The Vector Class

Description:

The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have the indexing ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 9
Provided by: Rebecca384
Learn more at: https://www.cs.unca.edu
Category:
Tags: class | space | vector

less

Transcript and Presenter's Notes

Title: The Vector Class


1
The Vector Class
  • An object of class Vector is similar to an array
    in that it stores multiple values
  • However, a vector
  • only stores objects
  • does not have the indexing syntax that arrays
    have
  • The methods of the Vector class are used to
    interact with the elements of a vector
  • The Vector class is part of the java.util package

2
The Vector Class
  • An important difference between an array and a
    vector is that a vector can be thought of as a
    dynamic, able to change its size as needed
  • Each vector initially has a certain amount of
    memory space reserved for storing elements
  • If an element is added that doesn't fit in the
    existing space, more room is automatically
    acquired

3
The Vector Class
  • The Vector class is implemented using an array
  • Whenever new space is required, a new, larger
    array is created, and the values are copied from
    the original to the new array
  • To insert an element, existing elements are first
    copied, one by one, to another position in the
    array
  • Therefore, the implementation of Vector in the
    API is not very efficient for inserting elements

4
Creating a Vector
  • Constructors
  • public Vector(int initialCapacity, int
    increment)
  • public Vector(int initialCapacity)
  • public Vector()
  • // gives an initial capacity of 10

5
Accessing an Element
  • public void setElementAt(Object newEelement, int
    index)
  • Analogous to Arrayindex newElement
  • public void elementAt(int index)
  • Analogous to Arrayindex
  • In both cases, index must be greater than zero
    and less than the current size of the vector

6
Adding Elements
  • public void addElement(Object newElement)
  • Adds specified element to the end of the vector
    and increases the size by one. Also increases
    the capacity if necessary.
  • public void insertElementAt(Object newElement,
    int index)
  • Inserts newElement at the specified index
    position and shifts the other existing elements
    to make room
  • public void setElementAt(Object NewElement, int
    index)
  • Overwrites the existing element

7
Removing Elements
  • public void removeElementAt(int index)
  • Removes element at the specified index and shifts
    all other elements down
  • public boolean removeElement(Object theElement)
  • Removes first occurrence of theElement and shifts
    all other elements down. Returns true if
    theElement is found and removed
  • public void removeAllElements()

8
Memory Management
  • public boolean isEmpty()
  • Returns true is size0
  • public int size()
  • Returns number of elements
  • public int capacity()
  • Returns capacity
Write a Comment
User Comments (0)
About PowerShow.com