简体   繁体   English

从另一个类向ArrayList添加元素

[英]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. 我只需要这个基本代码,我需要帮助将员工数据添加到另一个类的ArrayList。 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. 但是现在,我只需要帮助将元素添加到我的其他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;
    }
}

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. 看起来它很可能在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;
      }
}

It may be better to name your arraylist employees or employeeList. 为arraylist员工或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). 并且就您的问题而言,ArrayList是通过引用传递的,因此您只需为子员工列表(员工)添加一个getter方法。

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: 或者你可以像这样向Employee添加一个额外的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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