PROGRAMS

Display ArrayList data using Foreach loop in Java

This post explains a simple ArrayList program to add employee details and to display ArrayList data using foreach loop in Java. If you would like to display the arrayList data on a JSP page, go through the following link.

 

//DisplayArrayList.java

package com.arraylist;

import java.util.*;

class Employee{
 public int id;
 public String name;
 public String city;
 public static int count = 0;

 //zero argument constructor. Gets called by default when an object is created for the Employee class
 public Employee(){}

 //Parameterized Constructor
 public Employee(int id, String name,String city)
 {
  super();
  this.id = id;
  this.name = name;
  this.city=city;
  count++;
 }

 public int getId() {
 return id;
 }

 public String getName() {
 return name;
 }

 public String getAddress() {
 return city;
 }
}

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

  for (Employee s : list) //Iterates as long as there are elements in the list. An object s is created of type Employee class.
  {
   System.out.print("ID, Name and City of the employee are : ");
   System.out.println(s.getId()+" "+s.getName()+" " +s.getAddress());
  }
 }//End of main() method
}//end of DisplayArrayList class

The output of the program is as shown in the following picture.

Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. A founder of Krazytech. It's been 10+ years since I am publishing articles and enjoying every bit of it. I want to share the knowledge and build a great community with people like you.

Leave a Comment

View Comments

  • hi, if i have a list of 50 items or employee, and need to display only 10 items in page, and if user want see another 10 items, need to click button to see next 10 items. how i can do that?

    • Hi Vishal,
      Sorry. Your question is not very clear to me.
      If you want to increase the ID by 1, you can do plus 1 and add the record to the list again.

  • hey if we create a display() method in employee class. how can we call that method in foreach loop
    in your java code

    • Please try the below method.
      You can call this function in the Employee class.

      void display(List employees)
      {
      for (Employee s : employees) //Iterates as long as there are elements in the list. An object s is created of type Employee class.
      {
      System.out.print("ID, Name and City of the employee are : ");
      System.out.println(s.getId()+" "+s.getName()+" " +s.getAddress());
      }
      }

      In Main class,
      Employee emp = new Employee();
      emp.display(list)

Share
Published by
Ravi Bandakkanavar

Recent Posts

Everything You Need To Know About Create React App

The configuration of resources can be a time-consuming and difficult operation while creating a React…

3 weeks ago

Causes of CyberCrime and Preventive Measures

          Rapid technological growth and developments have provided vast areas of…

1 month ago

How Data Lineage will Improve Business Practices

How often have you thought about changing the way that you store and use data?…

1 month ago

10 Dominating Programming Language for Mobile App In 2022

Programming Languages are a set of rules that aid in transforming a concept into a…

1 month ago

What is Serverless Edge Computing? Features | Benefits

Serverless edge computing is a new technology with a lot of promise, but it can…

1 month ago

Are Your Accounts Protected Against Cyberattacks?

Do any of your passwords include personal names, date of birth, or pet names? If…

2 months ago