Amity School of Engineering & Technology (CSE)
[Link] package
Amity School of Engineering & Technology (CSE)
Hierarchy of Collection Framework
• A Collection represents a
single unit of objects, i.e.,
a group.
• [Link] package
contains all
the classes and interfaces
for the Collection
framework.
Amity School of Engineering & Technology (CSE)
Vector class
• Vector is like the dynamic array which can grow or shrink its
size.
• It is found in the [Link] package
• Vector is different from Arrays in two ways:
– Vector is synchronized
– It contains many legacy methods which are not part of Collection
framework.
Amity School of Engineering & Technology (CSE)
Contd…
A VECTOR CAN BE DECLARED A VECTOR WITHOUT SIZE CAN EVEN WHEN SIZE IS
WITHOUT SPECIFYING SIZE ACCOMMODATE ANY NO OF SPECIFIED, ITEMS CAN BE
EXPLICITLY. ITEMS ADDED INTO VECTOR.
Amity School of Engineering & Technology (CSE)
Contd…
Primitive data types
may be converted into
Vectors can not handle
objects using the
primitive data types like
Wrapper classes
int, float, char, double
contained in [Link]
package.
Amity School of Engineering & Technology (CSE)
Vector Constructors
Amity School of Engineering & Technology (CSE)
Example
import [Link];
public class VectorDemo {
public static void main(String[] args) {
Vector v1=new Vector();
Vector v2=new Vector(15);
Vector v3=new Vector(10,5);
[Link]([Link]() + " "+[Link]());
[Link]([Link]() + " "+[Link]());
[Link]([Link]() + " "+[Link]());
}
}
Amity School of Engineering & Technology (CSE)
Example
public class VectorDemo {
public static void main(String[] args) {
Vector v1=new Vector();
[Link](5);
[Link]("hi");
[Link](56);
[Link]("hello");
[Link](5);
[Link]("hi");
[Link](56);
[Link]("hello");
[Link]([Link]()+ " "+ [Link]());
}
}
Example Amity School of Engineering & Technology (CSE)
import [Link];
public class VectorDemo {
public static void main(String[] args) {
Vector v1=new Vector(12);
[Link]("Java");
[Link]("C");
[Link]("C++");
[Link](1);
[Link](2);
[Link]("Java");
[Link]("C");
[Link]("C++");
[Link](1);
[Link](2);
[Link](1);
[Link](2);
[Link](1.3);
[Link]([Link]()+ " "+[Link]());
}
}
Amity School of Engineering & Technology (CSE)
Specific type
Vector<Integer> v1=new Vector<Integer>();
Vector<String> v2=new Vector<String>();
Vector<Person> v3=new Vector<Person>();
Amity School of Engineering & Technology (CSE)
Example
public class VectorDemo {
public static void main(String[] args) {
Vector<Integer> v1=new Vector<Integer>();
[Link](5);
[Link]("hi"); //error
[Link](56);
[Link]("hello"); //error
}}
Amity School of Engineering & Technology (CSE)
Vector Methods
add(): It is used to append the specified element in the given vector.
addElement(): It is used to append the specified component to the end of this vector. It increases the
vector size by one.
capacity(): It is used to get the current capacity of this vector.
clear(): It is used to delete all of the elements from this vector.
clone(): It returns a clone of this vector.
elementAt(): It is used to get the component at the specified index.
firstElement(): It is used to get the first component of the vector.
Amity School of Engineering & Technology (CSE)
Contd…
• hashCode(): It is used to get the hash code value of a vector.
• get(): It is used to get an element at the specified position in the vector.
• indexOf(): It is used to get the index of the first occurrence of the specified
element in the vector. It returns -1 if the vector does not contain the
element.
• insertElementAt(): It is used to insert the specified object as a component
in the given vector at the specified index.
• remove(): It is used to remove the specified element from the vector. If the
vector does not contain the element, it is unchanged.
• removeAllElements(): It is used to remove all elements from the vector
and set the size of the vector to zero.
Amity School of Engineering & Technology (CSE)
Example
public class VectorDemo {
public static void main(String[] args) {
Vector<Integer> v1=new Vector<Integer>();
[Link](5);
[Link](56);
[Link](7);
[Link](15);
[Link]("Value at 0: "+ [Link](0));
}
}
Amity School of Engineering & Technology (CSE)
Another Example
public class VectorDemo {
public static void main(String[] args) {
Vector<Integer> v1=new Vector<Integer>();
[Link](5);
[Link](56);
[Link](7);
[Link](15);
[Link]("Value at 0: "+ [Link](3));
[Link]("Value at 0: "+ [Link]());
[Link](0);
[Link]("Value at 0: "+ [Link]());
}
}
Amity School of Engineering & Technology (CSE)
ArrayList
Amity School of Engineering & Technology (CSE)
Introduction
The ArrayList class
ArrayList supports
extends
dynamic arrays
AbstractList and
that can grow as
implements the List
needed.
interface.
Array lists are
created with an
initial size. When
When objects are
this size is
removed, the array
exceeded, the
may be shrunk.
collection is
automatically
enlarged.
Amity School of Engineering & Technology (CSE)
Amity School of Engineering & Technology (CSE)
Constructors
Amity School of Engineering & Technology (CSE)
Example 1
import [Link].*;
public class ArrayListDemo{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
[Link](“Java");//Adding object in arraylist
[Link](“C++");
[Link](“C");
[Link](“Python");
//Printing the arraylist object
[Link](list);
}
}
Amity School of Engineering & Technology (CSE)
Example 2
import [Link].*;
public class ArrayListDemo{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
[Link](“Java");//Adding object in arraylist
[Link](“C++");
[Link](“C");
[Link](“Python");
//Traversing list through Iterator
Iterator itr=[Link](); //getting the Iterator
while([Link]()){ //check if iterator has the elements
[Link]([Link]());//printing the element and move to next
}
}
}
Amity School of Engineering & Technology (CSE)
Example 3
//accessing the element
[Link]("Returning element: "+[Link](1));//it will return the 2nd
element, because index starts from 0
//changing the element
[Link](1,“C#");
//Traversing list
for(String prog:list)
[Link](prog);
Amity School of Engineering & Technology (CSE)
//Sorting the list
[Link](list1);
Amity School of Engineering & Technology (CSE)
Exercise
• Write a program to compare two arraylists.