[英]How do I search my ArrayList for a specific persons details? How would i then edit or remove them?
我有一個 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);
}
}
}
銷售人員類:
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;
}
}
那么你所嘗試的將負責在銷售人員List Of SalesPerson Object
中搜索部分並顯示更正。 您可以像下面的代碼一樣做更多的修改來處理刪除部分。
銷售人員Main.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;
}
}
試試上面的代碼,希望你知道在哪里改變更多。
假設一個 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);
}
您可以編輯它以搜索多個值或將其放入帶有搜索值等參數的方法中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.