Source Code : Store user-defined objects in arraylist

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Store user-defined objects in arraylist

      
import java.util.ArrayList;
public class MainClass {
  public static void main(String[] a) {
    ArrayList<Employee> emps = new ArrayList<Employee>();
    emps.add(new Employee("A", "G"));
    emps.add(new Employee("T", "A"));
    emps.add(new Employee("K", "J"));

    System.out.println(emps);

    Employee e = emps.get(1);
    e.setLastName("new");

    System.out.println(emps);
  }
}

class Address {
}

class Employee {
  private String lastName;

  private String firstName;

  private Double salary;

  public Address address;

  public Employee(String lastName, String firstName) {
    this.lastName = lastName;
    this.firstName = firstName;
    this.address = new Address();

  }

  public String getLastName() {
    return this.lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getFirstName() {
    return this.firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public double getSalary() {
    return this.salary;
  }

  public void setSalary(double salary) {
    this.salary = salary;
  }
}

   
    
    
    
    
  

Thank with us