ArrayList Class

ArrayList Class Simple Example

In our last blog, we show how to use ArrayList class in java program.

In this blog we will see simple ArrayList class example with Employee.

How to add Employee class’s object into ArrayList.

How to display Employee data using ArrayList using foreach concepts.

 

Let’s see Example on ArrayList Class

 

import java.util.*;

class Employee

{

                                int Eno;

                                String name,address;

 

                                Employee(){}

                                Employee(int Eno,String name,String address)

                                {

                                                this.Eno = Eno;

                                                this.name = name;

                                                this.address = address;

                                }

                                void displayData(ArrayList<Employee> list)

                                {

 

 

                                                for(Employee ob : list)

                                                {

                                                                System.out.println(“Employee:”);

                                                                System.out.println(“No : ” + ob.Eno);

                                                                System.out.println(“Name : ” + ob.name);

                                                                System.out.println(“Address : ” + ob.address);

                                                                System.out.println(“—————–“);

                                                }

                                }

}

class collection2

{

                public static void main(String args[])

                {

                                Employee obj1 = new Employee(101,”Dipak Mehta”,”Ahmedabad”);

                                Employee obj2 = new Employee(102,”Ronak Vyas”,”Surat”);

                                Employee obj3 = new Employee(103,”Srusti Mehra”,”Delhi”);

 

                                ArrayList<Employee> list = new ArrayList<Employee>();

                                list.add(obj1);

                                list.add(obj2);

                                list.add(obj3);

 

                                // Display Data using ArrayList

                                for(Employee ob : list)

                                {

                                                System.out.println(“Employee:”);

                                                System.out.println(“No : ” + ob.Eno);

                                                System.out.println(“Name : ” + ob.name);

                                                System.out.println(“Address : ” + ob.address);

                                                System.out.println(“—————–“);

                                }

 

                                System.out.println(“\n\nOther Process\n\n”);

                                Employee temp = new Employee();

                                // Call display method using temp object

                                temp.displayData(list);

                }

}

 

Output

List of Employee with Eno, Name And Address Details

 

Video on simple ArrayList Example


For more videos on Java visit our YouTube Channel

 

Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

306total visits,0visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint