Vector Arraylist
Vector Arraylist
Java.util package
Amity School of Engineering & Technology (CSE)
• java.util 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 java.util 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…
Vector Constructors
Amity School of Engineering & Technology (CSE)
Example
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
Vector v1=new Vector();
Vector v2=new Vector(15);
Vector v3=new Vector(10,5);
Example
public class VectorDemo {
public static void main(String[] args) {
Vector v1=new Vector();
v1.add(5);
v1.add("hi");
v1.add(56);
v1.add("hello");
v1.add(5);
v1.add("hi");
v1.add(56);
v1.add("hello");
System.out.println(v1.size()+ " "+ v1.capacity());
}
}
Example Amity School of Engineering & Technology (CSE)
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
Vector v1=new Vector(12);
v1.add("Java");
v1.add("C");
v1.add("C++");
v1.add(1);
v1.add(2);
v1.add("Java");
v1.add("C");
v1.add("C++");
v1.add(1);
v1.add(2);
v1.add(1);
v1.add(2);
v1.add(1.3);
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>();
v1.add(5);
v1.add("hi"); //error
v1.add(56);
v1.add("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.
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>();
v1.add(5);
v1.add(56);
v1.add(7);
v1.add(15);
System.out.println("Value at 0: "+ v1.get(0));
}
}
Amity School of Engineering & Technology (CSE)
Another Example
public class VectorDemo {
public static void main(String[] args) {
Vector<Integer> v1=new Vector<Integer>();
v1.add(5);
v1.add(56);
v1.add(7);
v1.add(15);
}
}
Amity School of Engineering & Technology (CSE)
ArrayList
Amity School of Engineering & Technology (CSE)
Introduction
Constructors
Amity School of Engineering & Technology (CSE)
Example 1
import java.util.*;
public class ArrayListDemo{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add(“Java");//Adding object in arraylist
list.add(“C++");
list.add(“C");
list.add(“Python");
//Printing the arraylist object
System.out.println(list);
}
}
Amity School of Engineering & Technology (CSE)
Example 2
import java.util.*;
public class ArrayListDemo{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add(“Java");//Adding object in arraylist
list.add(“C++");
list.add(“C");
list.add(“Python");
//Traversing list through Iterator
Iterator itr=list.iterator(); //getting the Iterator
while(itr.hasNext()){ //check if iterator has the elements
System.out.println(itr.next());//printing the element and move to next
}
}
}
Amity School of Engineering & Technology (CSE)
Example 3
//accessing the element
System.out.println("Returning element: "+list.get(1));//it will return the 2nd
element, because index starts from 0
//Traversing list
for(String prog:list)
System.out.println(prog);
Amity School of Engineering & Technology (CSE)
Exercise
• Write a program to compare two arraylists.