Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

CO_IF_22412_CO2

Manisha Pokharkar, Lecturer, Vidyalankar Polytechnic

Date: 12 February 2021

<<MSBTE LEAD>>
Unit 02 :
Derived Syntactical
Constructs in Java

Written by

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Unit Outcome 5: Develop
programs using vectors and
wrapper classes for the given
problem

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Learning Outcome 5b : Student
should understand the concept of
Vector

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
What we will learn today

1 What is Vector Key takeaways


2 Methods of Vector Concept of Vector

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar
Polytechnic, Mumbai

Page 5 Maharashtra State Board of Technical Education


Learning Objective/ Key learning

► Understand what is Vector.


► Understand methods of Vector

Page 6 Maharashtra State Board of Technical Education


Vector

► Vector class is in java.util package of java.


► Vector is dynamic array which can grow automatically according to the requirement.
► Vector does not require any fix dimension like String array and int array.
► Vectors are used to store objects that do not have to be homogeneous.

Page 7 Maharashtra State Board of Technical Education


Declaring Vector

► Vectors are created like arrays.


► Vector can be created using constructor of Vector class.
► Vector cannot directly store simple data types, so objects are created to store in a Vector class.
► The objects stored in Vector can be retrieved using an index value.
► Vector class defines three different constructors:

1) Vector list = new Vector();


This constructor creates a default vector, which has an initial size of 10.
2) Vector list = new Vector(int size);
This constructor accepts an argument that equals to the required size, and creates a vector whose initial capacity is
specified by size.
3) Vector list = new Vector(int size, int incr);
create vector with initial size and whenever it need to grows; it grows by value specified by increment capacity. If
Vector has to grow and increment is not specified, Vector doubles its capacity when it grows. The vector class
supports a number of methods that can be used to manipulate the vectors created.

Page 8 Maharashtra State Board of Technical Education


Methods of Vector Class

Methods Task performed


firstElement() It returns the first element of the vector.
lastElement() It returns last element.
addElement(item) Adds the item specified to the list at the end.
elementAt(int index) Gives the name of the object present at index position.
size() Gives the number of objects present.
setSize(int newSize) Sets the size of this vector.
Set(int index, object Replaces the element at the specified position in the vector
element) with the specified element.
capacity() Returns the current capacity of this vector.
Boolean Contains(object Tests if the specified object is a component in the vector.
elem)
clear() Removes all of the elements from this vector
remove(int index) Removes the element at the specified position in this vector.
remove(Object o) Removes the first occurrence of the specified element in this
vector, If the vector does not contain the element, it is
unchanged.
Page 9 Maharashtra State Board of Technical Education
removeRange(int fromIndex, int This method removes from this List all of the elements whose index is
toIndex) between fromIndex, inclusive and toIndex, exclusive.

removeElement(item) Removes the specified item from the list.


removeElementAt(n) Removes the item stored in the nth position of the list.
removeAllElements() Removes all the elements in the list.
copyInto(array) Copies all items from list of array.
insertElementAt(item, n) Inserts the item at nth position.
boolean contains(Object elem) Tests if the specified object is a component in this vector.
elements() It returns an enumeration of the element.
hasMoreElements() It checks if this enumeration contains more elements or not.
nextElement() It checks the next element of the enumeration.
int indexOf(Object o) This method returns the index of the first occurrence of the specified
element in this vector, or -1 if this vector does not contain the
element.
int indexOf(Object o, int index) This method returns the index of the first occurrence of the specified
element in this vector, searching forwards from index, or returns -1 if
the element is not found.
int lastIndexOf(Object o) This method returns the index of the last occurrence of the specified
element in this vector, or -1 if this vector does not contain the
element.
Page 10 Maharashtra State Board of Technical Education
Difference between Array and Vector
Array Vector

An array is a structure that holds multiple values of the The Vector is similar to array holds multiple objects and
same type. like an array; it contains components that can be accessed
using an integer index.
An array is a homogeneous data type where it can hold Vectors are heterogeneous. You can have objects of
only objects of one data type. different data types inside a Vector.
After creation, an array is a fixed-length structure. The size of a Vector can grow or shrink as needed to
accommodate adding and removing items after the Vector
has been created.
Array can store primitive type data element. Vector are store non-primitive type data element.
Array is unsynchronized i.e. automatically increase the size Vector is synchronized i.e. when the size will be exceeding
when the initialized size will be exceed. at the time; vector size will increase double of initial size.
Declaration of an array : Declaration of Vector:
int arr[] = new int [10]; Vector list = new Vector(3);
Array is the static memory allocation. Vector is the dynamic memory allocation.
Array allocates the memory for the fixed size ,in array Vector allocates the memory dynamically means according
there is wastage of memory. to the requirement no wastage of memory.
No methods are provided for adding and removing Vector provides methods for adding and removing
elements. elements.
In array wrapper classes are not used. Wrapper classes are used in vector.
Page 11 Array is not a class. Maharashtra State Board of Technical Education Vector is a class.
Quick Revision

1)______method gives the name of the object present at index position.


a) element(int index)
b) elementAt(int index)
c) both a and b
d) None of the mentioned

2) Vector class is present in ________ package.


a) java.lang
b) java.util
c) java.io
d) java.net

Page 12 Maharashtra State Board of Technical Education

You might also like