[go: up one dir, main page]

0% found this document useful (0 votes)
4 views24 pages

Vector Arraylist

The document provides an overview of the Java Collections Framework, focusing on the java.util package and specifically the Vector and ArrayList classes. It explains the characteristics of the Vector class, including its dynamic sizing, synchronization, and methods, along with examples of how to use it. Additionally, it introduces the ArrayList class, highlighting its dynamic array capabilities and providing examples of its usage and manipulation.

Uploaded by

mdivyansh866
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

Vector Arraylist

The document provides an overview of the Java Collections Framework, focusing on the java.util package and specifically the Vector and ArrayList classes. It explains the characteristics of the Vector class, including its dynamic sizing, synchronization, and methods, along with examples of how to use it. Additionally, it introduces the ArrayList class, highlighting its dynamic array capabilities and providing examples of its usage and manipulation.

Uploaded by

mdivyansh866
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Amity School of Engineering & Technology (CSE)

Java.util package
Amity School of Engineering & Technology (CSE)

Hierarchy of Collection Framework


• A Collection represents a
single unit of objects, i.e.,
a group.

• 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…

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 java.lang
package.
Amity School of Engineering & Technology (CSE)

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);

System.out.println(v1.size() + " "+v1.capacity());


System.out.println(v2.size() + " "+v2.capacity());
System.out.println(v3.size() + " "+v3.capacity());
}
}
Amity School of Engineering & Technology (CSE)

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);

System.out.println(v1.size()+ " "+v1.capacity());


}
}
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>();
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.

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>();
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);

System.out.println("Value at 0: "+ v1.elementAt(3));


System.out.println("Value at 0: "+ v1.firstElement());
v1.remove(0);
System.out.println("Value at 0: "+ v1.firstElement());

}
}
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 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

//changing the element


list.set(1,“C#");

//Traversing list
for(String prog:list)
System.out.println(prog);
Amity School of Engineering & Technology (CSE)

//Sorting the list


Collections.sort(list1);
Amity School of Engineering & Technology (CSE)

Exercise
• Write a program to compare two arraylists.

You might also like