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;
}
}
# 1 楼答案
假设销售人员类别如下:
}
您可以执行以下操作来搜索要删除的特定文件:
您可以编辑它来搜索多个值,或者将其放入一个带有搜索值等参数的方法中
# 2 楼答案
您尝试的内容将负责在
List Of SalesPerson Object
中搜索部分并显示更正。你们也可以做一些修改,比如下面的代码来处理remove part销售人员主。java
}
销售人员。java
}
试试上面的代码,希望你知道在哪里可以做更多的改变