Employee.java public class Employee
{
private String name;
private int age;
private int salary;
public Employee( String name, int age, int salary )
{
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge( int age )
{
this.age = age;
}
public int getSalary()
{
return salary;
}
public void setSalary( int salary )
{
this.salary = salary;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
+ "]";
}
}
ArrayListExample.java import java.util.ArrayList;
import java.util.List;
/*
* Storing user-defined class objects.
*/
public class ArrayListExample
{
public static void main(String[] args)
{
List<Employee> list = new ArrayList<Employee>();
Employee john = new Employee("John", 32, 40000);
Employee david = new Employee("David", 42, 80000);
Employee peter = new Employee("Peter", 52, 150000);
list.add(john);
list.add(david);
list.add(peter);
/*
* Using for each loop getting each employee object from the list
*/
for (Employee employee : list)
{
System.out.println(employee.toString());
}
}
}