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

Author: Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. It has been 6 years since I have been publishing articles and enjoying every bit of it. I want to share knowledge and build a great community with people like you.

Suggested read for you:

Did it help? Comment here..