如何从对象数组列表中删除项目?

时间:2019-04-13 20:12:01

标签: java object arraylist

我是Java(netbeans)编程的新手,在尝试了多种选择之后,我无法从数组列表中删除用于员工记录的项目。

我需要接受用户输入的名字和姓氏,ID,年薪和开始日期,然后将此信息添加到要显示的数组中。我可以很容易地添加和显示信息,但是使用相同的方法删除项目对我来说不起作用。进行stocking.add()可以正常工作,但不会执行inventory.remove()。这是我现在所做的,使用了为此活动必须使用的技术。

ArrayList <records> inventory = new ArrayList <records>();

    class records {
        String IDNumber, firstName, lastName, annualSalary, startDate;
        records (String temp1, String temp2, String temp3, String temp4, String temp5) {
            IDNumber = temp1;
            firstName = temp2;
            lastName = temp3;
            annualSalary = temp4;
            startDate = temp5;
        }
    }


    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

    String IDNumber, firstName, lastName, annualSalary, startDate;

    records eRec;

    IDNumber = IDNumberInput.getText();
    firstName = firstNameInput.getText();
    lastName = lastNameInput.getText();
    annualSalary = annualSalaryInput.getText();
    startDate = startDateInput.getText();

    eRec = new records(IDNumber, firstName, lastName, annualSalary, startDate);
    inventory.add(eRec);

    }                                         

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    String IDNumber, firstName, lastName, annualSalary, startDate;

    records eRec;

    IDNumber = IDNumberInput.getText();
    firstName = firstNameInput.getText();
    lastName = lastNameInput.getText();
    annualSalary = annualSalaryInput.getText();
    startDate = startDateInput.getText();

    eRec = new records(IDNumber, firstName, lastName, annualSalary, startDate);
    inventory.remove(eRec);

    }                                            

    private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

    String temp="";

        for (int x=0; x<=inventory.size()-1; x++) {
            temp = temp + "- " + inventory.get(x).firstName + " "
                    + inventory.get(x).lastName + " — ID Number: "
                    + inventory.get(x).IDNumber + "\nAnnual Salary: $"
                    + inventory.get(x).annualSalary + " — startDate: "
                    + inventory.get(x).startDate + "\n";
        }
        outputArea.setText(temp);   

    }                                          

3 个答案:

答案 0 :(得分:0)

在这种情况下,当您尝试删除一个元素时,必须牢记要删除与传入参数完全相同的对象,但要做到这一点,必须实现2种方法,这些方法将对您有所帮助过程中需要您,并且您必须先了解。

这些方法是equals()和hashCode(),可以在herehere中找到简要说明。

现在谈论您的示例,您的记录类必须实现这两种方法,如下所示:

    @Override
    public boolean equals(Object o) {
    if (this == o) {
        return true;
    }

    if (o == null || getClass() != o.getClass() || !(o instanceof Records)) {
       return false;
    }

    Records r = (Records) o;

    return (IDNumber.equals(r.getIDNumber()) && 
        firstName.equals(r.getFirstName()) && 
        lastName.equals(r.getLastName()) && 
        annualSalary.equals(r.getAnnualSalary()) && 
        startDate.equals(r.getStartDate()));
    }

    @Override
    public final int hashCode() {
        int result = 17;
        if (IDNumber != null) {
            result = 31 * result + IDNumber.hashCode();
        }
        if (firstName != null) {
            result = 31 * result + firstName.hashCode();
        }
        if (lastName != null) {
            result = 31 * result + lastName.hashCode();
        }
        if (annualSalary != null) {
            result = 31 * result + annualSalary.hashCode();
        }
        if (startDate != null) {
            result = 31 * result + startDate.hashCode();
        }
        return result;
   }

这样做之后,您就可以完成您想做的事情,通过像输入操作一样将值通知到inputTexts中来删除元素。

答案 1 :(得分:0)

如果您正在使用Set和Maps,则应添加equals和hashcode实现。

我是Objects Util类的忠实拥护者。它的集成也为您执行nullchecks。代码看起来也更干净。

public class Records {

    String idNumber, firstName, lastName, annualSalary, startDate;

    Records(String idNumber, String firstName, String lastName, String annualSalary, String startDate) {
        this.idNumber = idNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.annualSalary = annualSalary;
        this.startDate = startDate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Records)) {
            return false;
        }
        Records that = (Records) o;
        return Objects.equals(idNumber, that.idNumber)
                && Objects.equals(firstName, that.firstName)
                && Objects.equals(lastName, that.lastName)
                && Objects.equals(annualSalary, that.annualSalary)
                && Objects.equals(startDate, that.startDate);
    }

    @Override
    public int hashCode() {
        return Objects.hash(idNumber, firstName, lastName, annualSalary, startDate);
    }
}

我又改变了一个记录类。通常,您应该使用UpperCamelCase编写类名,并使变量名以小camelCase开头。为了保持整洁,请对参数,类名和变量使用有意义的名称。稍后,如果变得越来越复杂,它将有助于阅读和理解代码。

答案 2 :(得分:-1)

通过removeButtonActionPerformed方法创建新记录的想法,您可以首先使用ArrayList.get()获取记录,然后使用inventory.remove删除记录。 另外,如果知道要删除的记录的位置,则可以直接使用inventory.remove(position)