【问题标题】:How do I search my ArrayList for a specific persons details? How would i then edit or remove them?如何在我的 ArrayList 中搜索特定人员的详细信息?然后我将如何编辑或删除它们?
【发布时间】:2019-09-20 03:02:55
【问题描述】:

我有一个 ArrayList,它要求用户输入,然后存储每个员工的详细信息。如何显示存储在此 ArrayList 中的所有内容或搜索特定员工?然后我将如何从 ArrayList 中编辑或删除该员工?我有两个类,一个是 salesPerson - 我有所有变量、setter、getter 和构造函数,而 salesPersonMain - 我有提示用户输入、存储到数组等的地方。

我可以将员工添加到 ArrayList 并在结束输入循环时查看它们。

public class salesPersonMain {

public static void main(String[] args) throws InputValidationException {

    Scanner input = new Scanner(System.in);
    List<salesPerson> sPerson = new ArrayList<salesPerson>();

    while (true) {


        System.out.println("Enter id (press 'q' to quit): ");
        String temp = input.nextLine();
        if (temp.equals("q")) break;

        int id = Integer.parseInt(temp);

        System.out.println("Enter first name:");
        String firstName = input.nextLine();

        System.out.println("Enter last name:");
        String lastName = input.nextLine();


        //save in array list
        sPerson.add(new salesPerson(id, firstName, lastName));

    }
    for (salesPerson salesPerson : sPerson) {
        System.out.println(salesPerson);
       }
    }
 }

salesPerson 类:

import java.util.ArrayList;

public class salesPerson{
    //create variables for sales person
    private int id;
    private String firstName;
    private String lastName;

    public salesPerson() {

    }

    //Setters and getters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) throws InputValidationException {
        if (firstName.matches("\\p{Upper}(\\p{Lower}){2,20}")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.firstName = firstName;
        }
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName)throws InputValidationException {
        if (lastName.matches("\\p{Upper}(\\p{Lower}){2,20}")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.lastName = lastName;
        }
    }



    public void setCurrentCarMake(String currentCarMake) throws InputValidationException {
        if (currentCarMake.matches("(\\p{Alpha}{2,14})")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.currentCarMake = currentCarMake;
        }
    }

    public String getCurrentCarModel() {
        return currentCarModel;
    }

    public void setCurrentCarModel(String currentCarModel) throws InputValidationException {
        if (currentCarModel.matches("(\\p{Alnum}{1,10})")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.currentCarModel = currentCarModel;
        }
    }



    //create constructor
    public salesPerson(int id, String firstName, String lastName) throws InputValidationException {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    @Override
    public String toString() {
        return id + ", " + firstName + " " + lastName;
    }
}

【问题讨论】:

  • 你能举例说明你想要搜索/删除的内容吗?现在这个问题太广泛了,无法回答。包括 salesPerson 类也会有所帮助。
  • 如果销售人员离开公司,我想搜索他们并将他们从 ArrayList 中删除。我打算开设一个客户课程,因此我希望能够更改客户详细信息,例如电话号码。
  • 如果您打算定期编辑/删除项目,我建议您使用某种Map,键是salesPerson 的ID,因此您拥有O(1) 访问权限,然后只需要一定要使用Map 方法做任何你想做的事情。

标签: java oop arraylist


【解决方案1】:

您所尝试的将负责搜索List Of SalesPerson Object 中的部分并进行显示更正。您也可以像下面的代码一样进行一些修改来处理删除部分。

SalesPersonMain.java

public class SalesPersonMain {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    CopyOnWriteArrayList<SalesPerson> sPerson = new CopyOnWriteArrayList<>(); //List will fail in case of remove due to ConcurrentModificationException

    while (true) {
        System.out.println("Enter id (press 'q' to quit): ");
        String temp = input.nextLine();
        if (temp.equals("q")) break;

        int id = Integer.parseInt(temp);

        System.out.println("Enter first name:");
        String firstName = input.nextLine();

        System.out.println("Enter last name:");
        String lastName = input.nextLine();
        sPerson.add(new SalesPerson(id, firstName, lastName));

    }
    //Search
    System.out.println("Enter String to search & display");
    String searchString = input.nextLine();
    for (SalesPerson salesPerson : sPerson) {
        if(salesPerson.search(searchString)!=null){
            System.out.println(salesPerson.toString());
        }
    }
    //Remove
    System.out.println("Enter String to search & remove");
    searchString = input.nextLine();
    for (SalesPerson salesPerson : sPerson) {
        if(salesPerson.search(searchString)!=null){
            System.out.println(salesPerson.toString()+" is removed from the List");
            sPerson.remove(salesPerson);
        }
    }
    //Display All
    System.out.println("Final List : ");
    for (SalesPerson salesPerson : sPerson) {
        System.out.println(salesPerson.toString());
    }
}

}

SalesPerson.java

public class SalesPerson {

public SalesPerson(Integer id, String firstName, String lastName) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
}

Integer id;
String firstName;
String lastName;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Override
public String toString() {
    return "{" +
            "'id':" + id +
            ", 'firstName':'" + firstName + '\'' +
            ", 'lastName':'" + lastName + '\'' +
            '}';
}

public SalesPerson search(String search){
    if(firstName.matches("(.*)"+search+"(.*)") || lastName.matches("(.*)"+search+"(.*)")){
        return this;
    }
    return null;
}

}

试试上面的代码,希望你能明白在哪里改变什么。

【讨论】:

  • 那么我将如何处理删除 SalesPerson 类的代码?
  • 看看我从您的问题中了解到的情况,即您希望根据某些搜索删除一个 SalesPerson 对象列表。与我尝试解释的相同,请检查主类中的注释行//Remove。如果您想要其他内容,请详细说明您在这里的意思是什么?另外,为什么不试试我上面写的代码,看看是否涵盖了你的场景。
  • 我已经尝试了您提供的代码,它似乎可以工作,直到我删除了一名销售人员,然后我再次显示 arraylist 并且它们仍然存在。如何删除它们,然后显示更新的数组列表?
  • Ya @JayMac 现在我已经修改了代码(删除代码中有错字)来处理删除用例。阅读关于 ConcurrentModificationException 部分以了解删除失败的原因。现在试试你应该能够得到你的工作最终名单。
  • 删除工作正常,但我无法查看最终列表,因为我收到了 ConcurrentModificationException 错误
【解决方案2】:

假设一个salesPerson类如:

public class salesPerson {


    private int id;
    private String firstName, lastName;

    public salesPerson() {}

    public salesPerson(int id, String firstName, String lastName)
    {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

您可以执行以下操作来搜索要删除的特定内容:

        int searchIndex = 0;

    for (int i = 0; i < sPerson.size(); i++) {
        salesPerson currentPerson = sPerson.get(i);
        //Prints current person
        System.out.println(currentPerson.getId() + " " + currentPerson.getFirstName() + " " + currentPerson.getLastName()); 
        if (currentPerson.getId() == 40) //Change 40 and getId to whatever you want to search for
        {
            searchIndex = i;
        }
    }
    //The remove is outside the loop because you don't want to change the index during the loop or you might run into problems
    sPerson.remove(searchIndex);
    }

您可以对其进行编辑以搜索多个值或将其放入带有搜索值参数的方法等中。

【讨论】:

    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2011-05-06
    • 2012-05-29
    • 2022-01-12
    • 2019-08-23
    相关资源
    最近更新 更多