简体   繁体   中英

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. This class is called "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.

Now in another class called "MenuOptionMethods" I have the addEmployee method and fireEmployee method and another method to show employees information. I created two arrayList in this class called employee and formerEmployee.

Whenever the user adds an employee I put that employee object in the arrayList called employee. 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. 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(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. 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);

}

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