[英]How to delete and add an element from an array based of the user input in JAVA?
[英]Delete/Read a specific Element from an Array by User Request in Java
有人可以告诉我我的公共Employee deleteEmployeeID和我的公共Employee readEmployeeID方法怎么了 。 用户应该能够删除或阅读有关他们要求的特定员工ID的更多信息。 我似乎找不到这两种方法有什么问题。
子类
package WorkIDServerStorage;
public class EmployeeList{
private Employee[] theEmployee;
private int arrayEmployee;
public EmployeeList(){
theEmployee = new Employee[100];
arrayEmployee = 0;
}
public EmployeeList(int arraySize){
theEmployee = new Employee[arraySize];
arrayEmployee = 0;
}
public void setTheEmployee(Employee[] inputTheEmployee){
theEmployee = inputTheEmployee;
}
public void setArrayEmployee(int inputArrayEmployee){
arrayEmployee = inputArrayEmployee;
}
public Employee[] getTheEmployee(){
return theEmployee;
}
public int getArrayEmployee(){
return arrayEmployee;
}
public Employee addEmployeeID(Employee employeeAdd){
return theEmployee[arrayEmployee++] = employeeAdd;
}
public Employee deleteEmployeeID(int employeeDelete){
for(int i = employeeDelete + 1; i < theEmployee.length; i++){
theEmployee[i-1] = theEmployee[i];
}
return null;
}
public Employee readEmployeeInfo(int employeeRead){
return theEmployee[employeeRead];
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
for(int x = 0; x < arrayEmployee; x++){
sb.append(theEmployee[x].toString()).append("\n");
}return sb.toString();
}
}
主班
package WorkIDServerStorage;
import java.util.Scanner;
public class EmployeeListTest{
public static void main(String[] args){
EmployeeList EmployeeListObject = new EmployeeList();
Scanner input = new Scanner(System.in);
System.out.println("► Initiating Employee Information Server Storage ◄");
System.out.println("► Type 'EXIT' at any moment to close the program ◄\n");
for(int x = 1; x < 6; x++){
Employee EmployeeObject = new Employee("Employee " + x, 1234 + x, 15 + x);
EmployeeListObject.addEmployeeID(EmployeeObject);
}
System.out.println(EmployeeListObject);
System.out.print("\nType 'ADD' to add a new employee\n");
System.out.print("Type 'DELETE' to delete an employee record\n");
System.out.print("Type 'READ' to read the data of an employee ");
String menuOption = input.next();
if(menuOption.equalsIgnoreCase("EXIT")){
EmployeeListObject.toString();
}else if(menuOption.equalsIgnoreCase("ADD")){
System.out.print("\nInsert employee ID: ");
String addEmployeeID = input.next();
EmployeeListObject.addEmployeeID(addEmployeeID);
}else if(menuOption.equalsIgnoreCase("DELETE")){
System.out.print("\nInsert employee ID: ");
String deleteEmployeeID = input.next();
EmployeeListObject.deleteEmployeeID(deleteEmployeeID);
}else if(menuOption.equalsIgnoreCase("READ")){
System.out.print("\nInsert employee ID: ");
String readEmployeeID = input.next();
EmployeeListObject.readEmployeeInfo(readEmployeeID);
}
}
}
我看到的主要问题,可能是也可能不是您的问题的原因,是在您的主班上。 观察当用户要求读取数据时,实际上什么都没有打印到控制台中。
线
EmployeeListObject.readEmployeeInfo(readEmployeeID);
调用方法
public Employee readEmployeeInfo(int employeeRead){
return theEmployee[employeeRead];
}
它只返回和Employee
对象,但从不打印任何内容。 因此,用户将永远无法读取员工数据。
您代码中的另一个问题是@popiandro的评论。 但是,我认为这不是导致程序中断的原因。
public Employee deleteEmployeeID(int employeeDelete){
for(int i = employeeDelete + 1; i < theEmployee.length; i++){
theEmployee[i-1] = theEmployee[i];
}
return null;
}
此方法将始终返回null
,这样做没有任何意义。 将方法设置为void
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.