简体   繁体   English

通过Java中的用户请求从数组中删除/读取特定元素

[英]Delete/Read a specific Element from an Array by User Request in Java

Can someone tell me whats wrong with my public Employee deleteEmployeeID and my public Employee readEmployeeID methods. 有人可以告诉我我的公共Employee deleteEmployeeID和我的公共Employee readEmployeeID方法怎么了 The user should be able to delete or read more information about a specific employee ID they request. 用户应该能够删除或阅读有关他们要求的特定员工ID的更多信息。 I can't seem to find whats wrong with my two methods. 我似乎找不到这两种方法有什么问题。

SubClass 子类

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();
    }
}

Main Class 主班

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);
        }      
    }
}

The main issue I see, which may or may not be the cause of your problem is in your main class. 我看到的主要问题,可能是也可能不是您的问题的原因,是在您的主班上。 Observe how when the user asks to Read data, nothing is ever actually printed into the console. 观察当用户要求读取数据时,实际上什么都没有打印到控制台中。

The line 线

EmployeeListObject.readEmployeeInfo(readEmployeeID);

Calls the method 调用方法

public Employee readEmployeeInfo(int employeeRead){
    return theEmployee[employeeRead];
}

Which just returns and Employee object but never prints anything. 它只返回和Employee对象,但从不打印任何内容。 Therefore the user will never be able to read employee data. 因此,用户将永远无法读取员工数据。


The other issue in your code is what @popiandro commented. 您代码中的另一个问题是@popiandro的评论。 However I do not think it is the one causing your program to break. 但是,我认为这不是导致程序中断的原因。

public Employee deleteEmployeeID(int employeeDelete){        
    for(int i = employeeDelete + 1; i < theEmployee.length; i++){
        theEmployee[i-1] = theEmployee[i];
    }
    return null;
}

This method will always return null , and there is no point in doing that. 此方法将始终返回null ,这样做没有任何意义。 Set the method to void . 将方法设置为void


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

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