Java class Vector - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Java class Vector

Description:

Before defining objects of Vector, import it. ... import java.util.*; Define Vector objects, use different constructors. Vector v1 = new Vector ... – PowerPoint PPT presentation

Number of Views:740
Avg rating:3.0/5.0
Slides: 8
Provided by: csUi
Category:
Tags: class | java | reimport | vector

less

Transcript and Presenter's Notes

Title: Java class Vector


1
Java class Vector
  • The Vector class implement a growable array of
    objects.
  • User can use integer index to access items in a
    vector, like an array.
  • A vector can expand or shrink as needed when add
    or delete items.
  • Online document
  • http//java.sun.com/j2se/1.4.2/docs/api/java/util/
    Vector.html

2
Fields of class Vector
  • elementData
  • Internal data array storing elements in the
    vector
  • elementCount
  • The number of elements stored in the vector. It
    is equal to the vectors size.
  • capacityIncrement
  • The amount by which the capacity of the vector is
    automatically incremented when it becomes full.

3
Define objects of Vector
  • Before defining objects of Vector, import it.
  • import java.util.Vector // import is like
    using namespace in C, C
  • or
  • import java.util.
  • Define Vector objects, use different constructors
  • Vector v1 new Vector()
  • // v1 is empty. Its initial capacity is 10 and
    capacityIncrement is 0.
  • Vector v2 new Vector(50)
  • // v2 is empty. Its initial capacity is 50 and
    capacityIncrement is 0.
  • Vector v3 new Vector (80, 5)
  • // v3 is empty. Its initial capacity is 80 and
    capacityIncrement is 5.

4
Methods in Vector
  • Add elements into a vector
  • void add(int index, Object element)
  • Add element at position index
  •  boolean add(Object o)
  • Append o at the end of the vector.

5
Methods in Vector
  • Remove elements from a Vector
  • boolean remove(Object obj)
  • Remove the first occurrence of obj from the
    vector
  • Return false if obj is not found.
  • Object remove(int index)
  • Remove element at location index and return
    it. 

6
Methods in Vector
  • Search elements in an vector
  • int indexOf(Object elem)
  • Search for the first occurrence of elem and
    return its index.
  • int indexOf(Object elem, int index)
  • Searches for the first occurence of elem,
    beginning the search at index.
  • Both return -1 if elem is not found.
  • More
  • int lastIndexOf(Object elem)
  • int lastIndexOf(Object elem, int index)

7
Use methods in Vector
  • Vector class provide lots of useful methods
  • int size()
  • Object set(int index, Object element)
  • Object get(int index)
  • void clear()
  • // Much more
  • Make good use them will make your coding much
    easier.
  • For instance,
  • // Output all elements in vector v1.
  • System.out.println(v1.toString())
Write a Comment
User Comments (0)
About PowerShow.com