1
陣列讀取特定元素誰能告訴我什麼是錯我的公務員deleteEmployeeID我公務員readEmployeeID方法。用戶應該能夠刪除或閱讀更多關於他們請求的特定員工ID的信息。我似乎無法找到我的兩種方法有什麼問題。刪除/由用戶請求在Java中
子類
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);
}
}
}
它在幹什麼,你想幹什麼?另外,您可能想要閱讀ArrayList。 – blm
deleteEmployeeID應該返回void。它也應該使'arrayEmployee - '。爲什麼你的readEmployee不起作用?你執行它時看到什麼錯誤? – popiandro
您應該提供一些我們可以運行的代碼來演示問題。 – JVon