Java 如何在ArrayList中搜索特定人员的详细信息?然后如何编辑或删除它们?
我有一个ArrayList,它要求用户输入,然后存储每个员工的详细信息。如何显示存储在此ArrayList中的所有内容或搜索特定员工?然后如何编辑此员工或将其从ArrayList中删除?我有两个类,一个salesPerson——在这里我有所有变量、setter、getter和constructor,还有salesPersonMain——在这里我有用户输入提示、存储到数组等 我可以将员工添加到ArrayList中,并在结束输入循环时查看他们Java 如何在ArrayList中搜索特定人员的详细信息?然后如何编辑或删除它们?,java,oop,arraylist,Java,Oop,Arraylist,我有一个ArrayList,它要求用户输入,然后存储每个员工的详细信息。如何显示存储在此ArrayList中的所有内容或搜索特定员工?然后如何编辑此员工或将其从ArrayList中删除?我有两个类,一个salesPerson——在这里我有所有变量、setter、getter和constructor,还有salesPersonMain——在这里我有用户输入提示、存储到数组等 我可以将员工添加到ArrayList中,并在结束输入循环时查看他们 public class salesPersonMain
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);
}
}
}
假设销售人员类别如下:
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);
}
int searchIndex=0;
对于(int i=0;i
您可以编辑此项以搜索多个值,或将其放入带有搜索值等参数的方法中。您所尝试的将负责搜索SalesPerson对象列表中的部分,并显示更正。您也可以像下面的代码那样做一些修改来处理删除部件 salersonMain.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());
}
}
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类也会很有帮助。如果一个销售人员离开了公司,我想搜索他们并将他们从ArrayList中删除。我打算建立一个customer类,以便能够更改客户详细信息,如电话号码。如果您打算定期编辑/删除项目,我建议您使用某种
地图
,关键是销售人员ID,因此您有O(1)访问权限,然后只需要使用这些方法来做你想做的一切。那么我该如何在SalesPerson类中删除代码呢?看看我从你的问题中了解到的,给定SalesPerson对象的列表,你想根据一些搜索删除一个。与我尝试解释的内容相同,检查main类中的注释行//Remove
。如果你想要其他的东西,那么详细说明你在这里的意思是什么?还有,为什么不自己尝试一下我上面写的代码,看看是否涵盖了您的场景。我已经尝试了您提供的代码,它似乎可以工作,直到我删除了一名销售人员,然后我再次显示arraylist,他们仍然在那里。如何删除它们,然后显示更新的arraylist?Ya@JayMac现在我已经修改了代码(删除代码中有拼写错误)来处理删除用例。阅读关于ConcurrentModificationException部分,了解删除失败的原因。现在,请尝试获取您的最终工作列表。删除正在工作,但我无法查看最终列表,因为我得到的是ConcurrentModificationException错误
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;
}