Display ArrayList items using Foreach loop

By | December 30, 2015

This JSP page displays the ArrayList items using foreach loop.

import java.io.*;
import java.util.*;

class Employee{
public int id;
public String name;
public String address;
public static int count = 0; 
//zero argument constructor
public Employee(){}
//parametric constructor
public Employee(int id, String name,String address) 
this.id = id;
this.name = name;
public int getId() {
return id;
public String getName() {
return name;
public String getAddress() {
return address;

public class DisplayArrayList {

public static void main(String[] args) throws Exception {

List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(1, "Ravi","Delhi"));
list.add(new Employee(2, "Raj","Mumbai"));
list.add(new Employee(3, "Rekha","Chennai"));
list.add(new Employee(4, "Ram","Siliguri"));
System.out.println(" ");
System.out.print("Enter Employee Id  ");
Scanner input=new Scanner(System.in);
int id = input.nextInt();
 for (Employee s : list)
   if(id == s.getId())
     System.out.println("Name and Address of employee are  ");
     System.out.print(s.getName()+" " +s.getAddress());
Please Share: Tweet about this on TwitterShare on FacebookShare on Google+Share on RedditPin on PinterestShare on LinkedInDigg thisShare on StumbleUponShare on TumblrBuffer this pageShare on VKEmail this to someone

Leave a Reply

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