Top Banner
04/29/10 1
14

04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

Jan 12, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 1

Page 2: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 2

Introduction to Vectors? . . .

• A vector is a dynamic array.

- It can be expanded and shrunk as required- A Component of a vector can be accessed using integer index.

• Vectors always stores the objects.

• Vector class is in the java.util package.

Page 3: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 3

Constructors in Vector class. . .

Basically, there are four constructors defined in the vector class.

Constructor Description

Vector() The constructor creates a default vector containing no elements

Vector(int capacity) Creates an empty vector with the initial capacity specified by the parameter ‘capacity’

Vector(int capacity,int increment)

Creates an empty vector with initial capacity and the increment specified by the parameters ‘capacity’ and ‘increment’ respectively

Vector(Collection c) Creates a vector containing the elements of the collection c. The elements are accessed in the order returned by the collection’s iterator.

Page 4: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

02/10/10 4

Memory allocation for vectors. . .

• All vectors start with an initial capacity defined by the programmer.

Vector v=new Vector(5,3);

• After this initial capacity is reached, the next time that attempt to store an object in the vector, the vector automatically allocates space for that object + extra room for additional objects.

0 1 2 3 4

3 4 8 1 5

0 1 2 3 49

Add new element

Page 5: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

02/10/10 5

Memory allocation for vectors (Cont). . .

The amount of extra space allocated during each reallocation is determined by the increment that specified when creating the vector. If the increment is not specified, the vector size is doubled by each allocation cycle.

3 4 8 1 5

0 1 2 3 4

9

5

Additional space allocated

Page 6: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 6

Methods defined in Vector class. . .

Method Description

void addElement(element) The object specified by the parameter ‘element’ is added to the vector.

int capacity() Returns the capacity of the vector

boolean contains(element) Returns true if element is contained by the vector. Returns false other wise.

void copyInto(Object arr[]) The elements contained in the invoking vector are copied into the array specified by the parameter arr[].

Page 7: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 7

Methods defined in Vector class (cont). . .

Method Description

Element elementAt(int index) Returns the element specified by the index

void ensureCapacity(int min) Sets the minimum capacity of the vector to the size specified by ‘min’.

Element FirstElement() Returns the first element in the vector.

int indexOf(Object element) Returns the index of first occurrence of the element. If the object is not in the vector, -1 is returned.

void insertElementAt(Element element, int index)

Adds the ‘element’ to the vector at the location specified by the ‘index’

Page 8: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

8

Methods defined in Vector class (Cont). . .

Method Description

boolean isEmpty() Returns true if the vector is empty, and returns false if it contains one or more elements.

Element lastElement() Returns the last element of the vector

int lastIndexOf(Object element)

Returns the index of the last occurrence of element. If the object is not in the vector, then returns -1

void removeAllElements() Empties the vector. After executing this method, the size of the vector becomes zero.

Page 9: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 9

Methods defined in Vector class (Cont). . .

Method Description

boolean removeElement(Object element)

Removes element from the vector. If more than one instance of the Object exists in the vector, then it is the first one that is removed. Returns true if successful and false if the object is not found.

void removeElement(int index)

Removes the element at the location specified by the index

void setElementAt(Element element, int index)

The ‘element’ is assigned to the location specified by the index

Page 10: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 10

Methods defined in Vector class (Cont). . .

Method Description

void setSize(int size) Sets the number of elements in the vector to the ‘size’. If the new size is less than the old size, elements are lost. If the new size is larger than the old size, null elements are added.

int size() Returns the number of elements currently in the vector

void trimToSize() Sets the vectors capacity equal to the number of elements that it currently holds.

Object get(int index) Retrieves the element at the location specified by the index

Page 11: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 11

Example (1) . . .

import java.util.Vector;

class Vector_Example{

public static void main(String arg[]){

Vector v=new Vector(5,3);v.addElement(4);v.addElement(“Kamal”);v.addElement(90.5);

System.out.println(v.size());}

}

import java.util.Vector;

class Vector_Example{

public static void main(String arg[]){

Vector v=new Vector(5,3);v.addElement(4);v.addElement(“Kamal”);v.addElement(90.5);

System.out.println(v.size());}

}

Page 12: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

04/29/10 12

Example (2) . . .

import java.util.Vector;

class Vector_Example{

public static void main(String arg[]){

Vector<Integer> v=new Vector<Integer>(5,3);v.addElement(4);v.addElement(5);v.addElement(9);

System.out.println(v.get(2));}

}

import java.util.Vector;

class Vector_Example{

public static void main(String arg[]){

Vector<Integer> v=new Vector<Integer>(5,3);v.addElement(4);v.addElement(5);v.addElement(9);

System.out.println(v.get(2));}

}

Page 13: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

13

Iterator interface . . .

The iterator inteface provides a standard means of iterating through a list of elements. There some methods defined by the iterator interface.

- public boolean hasNext()

This method determines whether the structure contains any more elements.

- public Object next()

Retrieves the next element in the structure. If there are no more elements, next() with throw a NoSuchElementException exception.

Page 14: 04/29/101. 2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.

14

Example (3) . . .

Vector<Integer> v=new Vector<Integer>(5,3);v.addElement(4);v.addElement(5);v.addElement(9);

//Defines an Iterator object for vector by method iterator()Iterator it=v.iterator();while(it.hasNext())

{System.out.println(it.next());}

Vector<Integer> v=new Vector<Integer>(5,3);v.addElement(4);v.addElement(5);v.addElement(9);

//Defines an Iterator object for vector by method iterator()Iterator it=v.iterator();while(it.hasNext())

{System.out.println(it.next());}