有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在ArrayList中搜索特定人员的详细信息?然后如何编辑或删除它们?

我有一个ArrayList,它要求用户输入,然后存储每个员工的详细信息。如何显示存储在此ArrayList中的所有内容或搜索特定员工?然后如何编辑此员工或将其从ArrayList中删除?我有两个类,一个salesPerson——在这里我有所有变量、setter、getter和constructor,还有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);
       }
    }
 }

销售人员类:

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

共 (2) 个答案

  1. # 1 楼答案

    假设销售人员类别如下:

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

    您可以编辑它来搜索多个值,或者将其放入一个带有搜索值等参数的方法中

  2. # 2 楼答案

    您尝试的内容将负责在List Of SalesPerson Object中搜索部分并显示更正。你们也可以做一些修改,比如下面的代码来处理remove part

    销售人员主。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());
        }
    }
    

    }

    销售人员。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;
    }
    

    }

    试试上面的代码,希望你知道在哪里可以做更多的改变