简体   繁体   中英

Adding elements to an ArrayList from another class

I just have this basic code where I need help adding employee data to an ArrayList of another class. I am just writing this code in preparation for an assignment, so don't bash my code too much. Essentially though, i'll be needing to add elements of employees and delete them eventually. But for now, I just need help adding the elements to my other Employee class. =]

public class main {
    private static Employee employee;   

    public static void main(String[] args) {
        employee = new Employee(10,10); 
        System.out.println(employee.toString());
    }
}

...............

import java.util.ArrayList;

public class Employee {
    public int employeeNum;
    public double hourRate;
    ArrayList<Employee> Employee = new ArrayList<>();

    public Employee(int employeeNum, double hourRate){
        this.employeeNum = employeeNum;
        this.hourRate = hourRate;
    }

    public String toString(){
        return ""+employeeNum+hourRate;
    }
}

Simple Example -

package com;

import java.util.ArrayList;

public class TestPage{

    public static void main(String[] args){
        Employee emp1, emp2;

        emp1 = new Employee();
        emp2 = new Employee();

        emp1.setName("MAK");
        emp2.setName("MICHELE");

        emp1.setAddress("NY");
        emp2.setAddress("WY");

        //and keep putting other information like this

        ArrayList<Employee> employee = new ArrayList<Employee>();
        employee.add(emp1);
        employee.add(emp2);

        System.out.println("emp1 name is : " + employee.get(0).getName());
        System.out.println("emp2 name is : " + employee.get(1).getName());

        System.out.println("emp1 address is : " + employee.get(0).getAddress());
        System.out.println("emp2 address is : " + employee.get(1).getAddress());
    }
}

class Employee{
    String name, address;
    int age, salary;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    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;
    }
}

It seems like what you're asking is based on one employee having sub-employees and that structurally that probably represents a hierarchy (Some commenters seem to be missing that point). But that's an assumption on my part. Based on that assumption.

A little bit of feedback to start on structure of your main class:

public class main {  
    public static void main(String[] args) {
        Employee employee = new Employee(10,10); 
        System.out.println(employee.toString());
    }
}

It seems to me that there's no reason to have a static instance variable for that root employee instance. You should try to limit the scope of variables where possible. It seems like it could very well be in the main() method's scope.

    public class Employee {
      public int employeeNum;
      public double hourRate;
      ArrayList<Employee> employees= new ArrayList<>();

      public Employee(int employeeNum, double hourRate){
         this.employeeNum = employeeNum;
         this.hourRate = hourRate;
      }

      public String toString(){
         return ""+employeeNum+hourRate;
      }

      public ArrayList<Employee> getEmployees() { 
           return this.employees;
      }
}

It may be better to name your arraylist employees or employeeList. I went with employees in this case because that convention is preferable.

And in relation to your question, ArrayList is pass by reference so you could just add a getter method for the sub-employee list (employees).

To add employees from your main method you could do something like

Employee rootEmployee = new Employee(5, 10.0); 
rootEmployee.getEmployees().add(new Employee(6, 5.0)); 

Or you could add an additional method to Employee like this:

public void addEmployee(Employee e) { 
   employees.add(e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM