Lab#02
Lab#02
LAB # 02
ArrayList
An ArrayList is like an array that resizes to fit its contents. Thus, it differs from array:
i) Size does not need to be specified at creation time.
ii) Grows in size as items added.
iii) ArrayLists, however, come with a selection of powerful methods whereas arrays
consist of length instance variable. When a list is created, it is initially empty.
Constructors
Creating ArrayList:
Methods
To access an element in the ArrayList, use the get() method and refer to the index
number.
To modify an element, use the set() method and refer to the index number.
To remove an element, use the remove() method and refer to the index number.
To remove all the elements in the ArrayList, use the clear() method.
To find out how many elements an ArrayList have, use the size() method.
Sample Program#1
import java.util.*;
class JavaExample{
public static void main(String args[]){
ArrayList<String> alist=new ArrayList<String>();
alist.add("Steve");
alist.add("Tim");
alist.add("Lucy");
alist.add("Pat");
alist.add("Angela");
alist.add("Tom");
//displaying elements
System.out.println(alist);
//displaying elements
System.out.println(alist);
}
}
ArrayList uses the Array data structure and it should be used where more search
operations are required. Here, we are going to discuss the affects on the performance of
insert, search, and delete operation of ArrayList. Below is an example of different
operations using ArrayList;
Sample program#2
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
It has time complexity O(1), but due to the added steps of creating a new array, its
worst-case complexity reaches to order of N.
Search by Value
When we search any value in ArrayList, we have to iterate through all elements. This
operation has O(N) time complexity.
Here we need to notice that array stores all values in a continuous memory location.
Iterating through continuous memory location is more performance efficient.
Remove by Value
It is similar to adding value at a given index. To remove an element by value in
ArrayList we need to iterate through each element to reach that index and then remove
that value. This operation is of O(N) complexity.
In ArrayList, we need to shift all elements after the index of the removed value to fill
the gap created.
Remove by Index
To remove by index, ArrayList find that index using random access in O(1) complexity,
but after removing the element, shifting the rest of the elements causes overall O(N)
time complexity.
We generally use Collections.sort() method to sort a simple array list. However if the
ArrayList is of custom object type then in such case you have two options for
sorting- comparable and comparator interfaces.
Sample Program #3
import java.util.*;
@Override
public int compareTo(Student comparestu) {
int compareage=((Student)comparestu).getStudentage();
return this.studentage-compareage;
}
@Override
public String toString() {
return "[ rollno=" + rollno + ", name=" + studentname + ", age=" + studentage +
"]";
}
}
Collections.sort(arraylist);
Vector
Vector implements a dynamic array. It is similar to ArrayList, but with two differences:
Vector is synchronized.
Vector contains many legacy methods that are not part of the collections
framework.
Vector proves to be very useful if you don't know the size of the array in advance or
you just need one that can change sizes over the lifetime of a program.
Creating Vector:
Sample Program#4
import java.util.*;
public class VectorExample {
public static void main(String args[]) {
//Create a vector
Vector<String> vec = new Vector<String>();
Java equals() and hashCode() methods are present in Object class. So every java
class gets the default implementation of equals() and hashCode().
Java hashCode() and equals() method are used in Hash table based
implementations in java for storing and retrieving data.
Sample Program#5
import java.io.*;
class Example {
public String name;
public int id;
this.name = name;
this.id = id;
}
@Override
public boolean equals(Object obj){
@Override
public int hashCode()
{
class Main{
public static void main (String[] args){
if(e1.hashCode() == e2.hashCode()) {
if(e1.equals(e2))
System.out.println("Both Objects are equal. ");
else
System.out.println("Both Objects are not equal. ");
}
else
System.out.println("Both Objects are not equal. ");
}
}
Lab Tasks
1. Write a program that initializes Vector with 10 integers in it. Display all the integers
and sum of these integers.
Home Tasks
2. Write a java program which takes user input and gives hashcode value of those
inputs using hashCode () method.
3. Scenario based
Create a java project, suppose you work for a company that needs to manage a list of
employees. Each employee has a unique combination of a name and an ID. Your goal
is to ensure that you can track employees effectively and avoid duplicate entries in your
system.
Requirements
a. Employee Class: You need to create an Employee class that includes:
b. Employee Management: You will use a HashSet to store employee records. This
will help you avoid duplicate entries.
c. Operations: Implement operations to:
4.Create a Color class that has red, green, and blue values. Two colors are considered
equal if their RGB values are the same