从另一个类向ArrayList添加元素

时间:2016-04-20 00:45:38

标签: java arraylist

我只有这个基本代码,我需要帮助将员工数据添加到另一个类的ArrayList。我只是编写这段代码以准备作业,所以不要过多地抨击我的代码。从本质上讲,我将需要添加员工元素并最终删除它们。但是现在,我只需要帮助将元素添加到我的其他Employee类中。 =]

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;
    }
}

2 个答案:

答案 0 :(得分:0)

简单示例 -

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;
    }
}

答案 1 :(得分:0)

您似乎要求的是基于一名员工拥有子员工,结构上可能代表一个层级(一些评论者似乎忽略了这一点)。但这是我的一个假设。基于这个假设。

从主要课程结构开始的一些反馈:

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

在我看来,没有理由为该根员工实例提供静态实例变量。您应该尽可能地限制变量的范围。看起来它很可能出现在main()方法的范围内。

    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;
      }
}

为arraylist员工或employeeList命名可能更好。在这种情况下我和员工一起去,因为这个惯例是可取的。

就您的问题而言,ArrayList是通过引用传递的,因此您只需为子员工列表(员工)添加一个getter方法。

要从主要方法添加员工,您可以执行类似

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

或者你可以像这样向Employee添加一个额外的方法:

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