简体   繁体   English

将对象从一个数组列表移动到另一个数组列表

[英]move an object from one array list to another

I have a class that has private variables such as employeeName and employeeNumber and methods to set and get employeeName and employeeNumber. 我有一类具有私有变量(例如employeeName和employeeNumber)以及用于设置和获取employeeName和employeeNumber的方法的类。 This class is called "EmployeesInformation". 该类称为“ EmployeesInformation”。 In this class I have two constructors. 在这个类中,我有两个构造函数。 One that gets employee's information such as EmployeesInformation(String name, String phoneNumber){...} and another one that gets the same information but also receives two additional bits of information such as String datefired and String reasonForLeave. 一个获取雇员的信息,例如EmployeesInformation(String name, String phoneNumber){...} ,另一个获取相同的信息,但又接收另外两个信息,例如String datefired和String reasonForLeave。

Now in another class called "MenuOptionMethods" I have the addEmployee method and fireEmployee method and another method to show employees information. 现在,在另一个名为“ MenuOptionMethods”的类中,我具有addEmployee方法和fireEmployee方法以及另一个用于显示员工信息的方法。 I created two arrayList in this class called employee and formerEmployee. 我在此类中创建了两个arrayList,分别称为employee和viousEmployee。

Whenever the user adds an employee I put that employee object in the arrayList called employee. 每当用户添加雇员时,我都会将该雇员对象放在名为employee的arrayList中。 When the user fires or removes an employee I want to take all of that employee's information, remove it from arrayList employee and add it to arrayList formerEmployee. 当用户解雇或删除员工时,我要获取该员工的所有信息,将其从arrayList员工中删除,并将其添加到arrayList agoEmployee。 That is where I'm having problems. 那就是我遇到问题的地方。 Can someone take a look at my code and tell me what's wrong with it? 有人可以看看我的代码并告诉我它怎么了吗?

public class menuOptionMethods {
Scanner sc = new Scanner(System.in);
private ArrayList<EmployeesInformation> employee;
private ArrayList<EmployeesInformation> formerEmployee;

public menuOptionMethods() {
    employee = new ArrayList<EmployeesInformation>();
    formerEmployee = new ArrayList<EmployeesInformation>();
}

public void addEmployee(String eName) {     
    String n = eName;
    System.out.println(" Enter date hired: ");
    String h = sc.next();
    System.out.println(" Enter employee's duty: ");
    String d = sc.next();
    System.out.println(" Enter employee's phone number: ");
    String pN = sc.next();
    System.out.println(" Enter employee's pay per hour: ");
    double pPH = sc.nextInt();
    System.out
            .println(" Enter any additional information about employee: ");
    String l = sc.next();
    EmployeesInformation e = new EmployeesInformation(n, h, d, l, pN, pPH);
    employee.add(e);
}

public void fireEmployee(String eName) {
    // System.out.println("Enter employee's name: ");
    // String name = eName;
    System.out.println("Reason for employee's leave?: ");
    String reason = sc.next();
    System.out.println("Enter date: ");
    String dF = sc.next();
for(int i=0; i<employee.size(); i++){
      if(employee.get(i).getEmployeName().contains(eName)){ 
          n = eName; 
          h = employee.get(i).getDateHired(); 
          d = employee.get(i).getEmployeDuty();
          pH = employee.get(i).getPhoneNumber(); 
          pPH = employee.get(i).getEmployePay(); 
          l = employee.get(i).getAdditionalInformation();
          employee.remove(i); 
          } 
      }
      EmployeesInformation fE = new  EmployeesInformation(n,h,d,l,pH,pPH,reason,dF); // ERROR HAPPENS HERE

}

} }

You can't remove element from list while iterating it with for loop (it will throw ConcurrentModificationException . To do that, you need to use iterator and call remove() method, eg: 在使用for循环进行迭代时,不能从列表中删除元素(它会抛出ConcurrentModificationException 。为此,您需要使用迭代器并调用remove()方法,例如:

for(Iterator<Employee> iterator = employees.iterator() ; iterator.hasNext();){
    Employee current = iterator.next();
    if(current.getName().equals(name)){
        iterator.remove();
        //Add into former employees' list
        break;
    }
}

This will remove from existing list. 这将从现有列表中删除。

In your for loop, you don't want to do any removing because the size of the arraylist will change, and that just creates whack that is throwing you off. 在您的for循环中,您不希望执行任何删除操作,因为arraylist的大小将发生变化,而这只会产生使您失望的重击。 Assuming every employee has a unique name, you could do something like this (note I simplified making all those new variable by just transferring that employee object from one arraylist to the other): 假设每个员工都有一个唯一的名称,则可以执行以下操作(请注意,通过将员工对象从一个数组列表转移到另一个数组,我简化了所有这些新变量的创建):

  int index;
  for(int i=0; i<employee.size(); i++){
  if(employee.get(i).getEmployeName().contains(eName)){ 
      formerEmployee.add(employee[i]); //date fired and reason fired can be added later
      index = i;
      break; 
      } 
  }
  employee.remove(i);

} }

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

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