Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

IMPORTANCE OF JAVA AND

COLLECTIONS - REAL TIME EXAMPLES

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
JAVA ARRAYLIST

• The Java array list class uses a dynamic array to store the elements.
The abstract class is inherited by this class and the list interface is
implemented by this class.
• The Java array list class can include duplicate elements and it
manages the insertion order.
• This class facilitates random access because array works at the index
basis and it is non-synchronized. In this class, the manipulation is
not fast because many shifting must take place if any element is
deleted from the array list.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
REAL-TIME EXAMPLES OF USING
ARRAYLIST IN JAVA

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Question
• Write a java program that contains an Employee class with the following
properties. eid (type is int), ename (type is String), Department (type is
Department). Now create another class named Department which contains the
properties did(type is int), dname(type is String) and designation (type is String).
Now create another class named EmployeeApp which contains the main method.
• Now write code that prompts the user to enter the Employee details like id,
name, and prompt Department details like department id, name and designation
details in ArrayList<Employee> object. Enter at least 3 employees and
department info print those details to the output by iterating through
Department ArrayList. Another important thing is that the property Department
in Employee class should be of type Department (which is a class).

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
First class ‘Employee’

public class Employee {


int eid;
String ename;
Department department;
public Employee(int eid, String ename, Department department)
{
this.eid = eid;
this.ename = ename;
this.department = department;
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Second class ‘Department’

public class Department {

int did;
String dname;
String designation;

public Department(int did, String dname, String designation) {

this.did = did;
this.dname = dname;
this.designation = designation;
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Final Class ‘EmployeeApp’
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeApp {
static ArrayList<Employee> list = new ArrayList<Employee>();
static void pushElement(Employee obj) {
list.add(obj);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many Employee's information to be stored?");
int count = sc.nextInt();
for (int i = 0; i <= count; i++) {
System.out.println("Enter Employee ID: ");
int eid = sc.nextInt();

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
System.out.println("Enter Employee Name: ");
String ename = sc.next();
System.out.println("Enter Department ID: ");
int did = sc.nextInt();
System.out.println("Enter Department Name");
String dname = sc.next();
System.out.println("Enter Department designation: ");
String designation = sc.next();
Department d = new Department(did, dname, designation);
Employee eobj = new Employee(eid, ename, d);
pushElement(eobj);
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
System.out.println("--- Employee Information ---");
for (Employee obj : list)
{
System.out.println("Employee ID: " + obj.eid); System.out.println("Employee Name: " +
obj.ename);
Department department = obj.department; System.out.println("Department ID: " +
obj.department.did); System.out.println("Department Name :" +
obj.department.dname);
System.out.println("Department designation " + obj.department.designation);
}
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Thank you!!!

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.

You might also like