防止数组列表中的重复项
Prevent duplicates in arraylist
无法弄清楚为什么此代码不能阻止重复的客户端,重复的是具有相同名称的客户端。
我知道这个问题有更好的解决方案。但我只是一个初学者,想通过下面的方式解决这个问题。感谢您的帮助...
import java.util.*;
public class Kund {
public static ArrayList<Kund> customerList = new ArrayList<>();
public static void addCustomer(){
System.out.println("Add a customer:");
String customerXName = Program.readString("Name of Customer: ");
String customerXAdress = Program.readString("Adress of Customer: ");
for (int index = 0; index < customerList.size(); index++) {
Customer customerobj = customerList.get(index);
if (customerobj.getName().equalsIgnoreCase(customerXName)) {
System.out.println("Customer with the given name already exists. Choose another name...");
addCustomer();
break;
}
}
Customer customerX = new Customer(customerXName, customerXAdress);
customerList.add(customerX);
System.out.println("The following customer has been registered: "
+ customerX);
System.out.println(customerList);
System.out.println();
}
如果您输入一个已存在于列表中的客户,循环会找到它并要求您输入一个新客户。但是,当你输入一个新客户后,你并没有重新开始循环,所以你不会检查你输入的新客户是否在列表中。
此外,每次发现客户已经存在时递归调用addCustomer
并不是一个好主意,因为一旦循环结束,就会添加客户。
你在那里使用递归,这就是问题所在。一旦你找到一个名字出现在列表中,你再次调用 addCustomer()
方法。在其中一个调用中,用户输入的名称不在列表中,您将其添加到列表中,然后从方法中 return 。
从上次调用 return 后,控件将到达上一个方法调用的堆栈,从那里继续 break
从循环开始,在循环外,它将客户添加到当前堆栈的名称和地址,这是重复的,但仍会添加。
通话轨迹是这样的:
addCustomer() // name = "A", found duplicate
addCustomer() // name = "B", found duplicate
addCustomer() // name = "C", no duplicate.
// add the customer with name = "C"
// return to the previous call
break the loop
// outside the loop, add customer with name = "B" to the list
// return to the previous call
break the loop
// outside the loop, add customer with name = "A" to the list
要解决此问题,您可以 return
从方法中代替使用 break
,或者更好地使用循环。
无法弄清楚为什么此代码不能阻止重复的客户端,重复的是具有相同名称的客户端。
我知道这个问题有更好的解决方案。但我只是一个初学者,想通过下面的方式解决这个问题。感谢您的帮助...
import java.util.*;
public class Kund {
public static ArrayList<Kund> customerList = new ArrayList<>();
public static void addCustomer(){
System.out.println("Add a customer:");
String customerXName = Program.readString("Name of Customer: ");
String customerXAdress = Program.readString("Adress of Customer: ");
for (int index = 0; index < customerList.size(); index++) {
Customer customerobj = customerList.get(index);
if (customerobj.getName().equalsIgnoreCase(customerXName)) {
System.out.println("Customer with the given name already exists. Choose another name...");
addCustomer();
break;
}
}
Customer customerX = new Customer(customerXName, customerXAdress);
customerList.add(customerX);
System.out.println("The following customer has been registered: "
+ customerX);
System.out.println(customerList);
System.out.println();
}
如果您输入一个已存在于列表中的客户,循环会找到它并要求您输入一个新客户。但是,当你输入一个新客户后,你并没有重新开始循环,所以你不会检查你输入的新客户是否在列表中。
此外,每次发现客户已经存在时递归调用addCustomer
并不是一个好主意,因为一旦循环结束,就会添加客户。
你在那里使用递归,这就是问题所在。一旦你找到一个名字出现在列表中,你再次调用 addCustomer()
方法。在其中一个调用中,用户输入的名称不在列表中,您将其添加到列表中,然后从方法中 return 。
从上次调用 return 后,控件将到达上一个方法调用的堆栈,从那里继续 break
从循环开始,在循环外,它将客户添加到当前堆栈的名称和地址,这是重复的,但仍会添加。
通话轨迹是这样的:
addCustomer() // name = "A", found duplicate
addCustomer() // name = "B", found duplicate
addCustomer() // name = "C", no duplicate.
// add the customer with name = "C"
// return to the previous call
break the loop
// outside the loop, add customer with name = "B" to the list
// return to the previous call
break the loop
// outside the loop, add customer with name = "A" to the list
要解决此问题,您可以 return
从方法中代替使用 break
,或者更好地使用循环。