java - 检查列表是否存在数据然后再次输入值
问题描述
我的主要目标是确定输入的号码是否在列表中不重复,否则用户需要更新新号码。系统将不断要求用户输入不重复的数字。
我目前正在努力获取逻辑以检查我的列表是否包含重复的 ID。如果有人输入了重复的 ID,则会提示他重新输入一个新号码。将再次检查新编号,直到系统满足没有重复元素为止。下面的函数返回一个整数,它将被添加到 main 方法中 Course 类型的 List 中。
以下是我的函数的片段:
public static int ifExist(List<Course> courselist, Iterator<Course> itr, int personid) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
boolean found = false;
boolean flag = false;
int personid2 = personid;
String value = null;
while (itr.hasNext()) {
Course courseItr = itr.next();
if(courseItr.getPersonID() == personid) {
found = true;
flag = true;
while(found == true) {
System.out.print("No duplicate number is accepted. Please enter another number: ");
do {
// must be a digit from 1 - 10,000
String digit = "\\d{1,10000}";
value = input.nextLine();
flag = value.matches(digit);
if (!flag) System.out.print("Please a number only!: ");
} while (!flag);
personid2 = Integer.parseInt(value);
if(personid2 != courseItr.getPersonID()) {
found= false;
}
}
}
}
return personid2;
}
执行 Course 程序时的输出如下所示。请注意,输入 no 1 表示添加课程列表。
Please select your choice: 1
Enter the person ID: 1
Enter the person name: Alysa
Enter the title of the course: Maths
Enter the year of joining: 2021
Enter the fee of the course: 20.50
New Course has successfully been added.
Please select your choice: 1
Enter the person ID: 1
No duplicate number is accepted. Please enter another number: 1
No duplicate number is accepted. Please enter another number: 1
No duplicate number is accepted. Please enter another number: 1
No duplicate number is accepted. Please enter another number: 2
Enter the person name: Maria
Enter the title of the course: Biology
Enter the year of joining: 2021
Enter the fee of the course: 25.99
New Course has successfully been added.
Please select your choice: 1
Enter the person ID: 2
No duplicate number is accepted. Please enter another number: 2
No duplicate number is accepted. Please enter another number: 2
No duplicate number is accepted. Please enter another number: 2
No duplicate number is accepted. Please enter another number: 1
Enter the person name: Peter
Enter the title of the course: Chemistry
Enter the year of joining: 2021
Enter the fee of the course: 50.50
New Course has successfully been added.
如上所示,它表明我的 ifExist 方法不起作用(试图让逻辑正确)。两个人的ID相同,比如1。
当我尝试显示课程列表时
Please select your choice: 3
Person ID: 1, Name: Alysa, Title: Maths, Year: 2021, Fee: $20.5.
Person ID: 2, Name: Maria, Title: Biology, Year: 2021, Fee: $25.99.
Person ID: 1, Name: Peter, Title: Chemistry, Year: 2021, Fee: $50.5.
我已经用谷歌搜索过了,但似乎我要么必须使用 Set 删除任何重复项,要么使用 equals/hashcode()。尽管如此,如果任何有经验的 Java 程序员帮助澄清或提供有关如何解决此问题的任何想法,我将不胜感激。
新增方法
public static void addCourse(List<Course> courselist, Course course) {
//check if the id is the same or not
ListIterator<Course> itr = courselist.listIterator();
try {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int personid, year;
String author, title;
double fee;
System.out.print("\nEnter the person ID: ");
personid = input.nextInt();
personid = ifExist(booklist, itr, personid);
course.setPersonDd(personid);
...
...
...
courselist.add(new Course(personid, author, title, year, fee));
System.out.println("New Course has successfully been added.");
} catch {
}
}
谢谢你。期待其他开发者的来信。
问候, Simone11
解决方案
关于你的第一种方法
问题在于迭代器。当用户输入1
之后2
,迭代器已经通过了 ID 的课程1
,因此无法检测到重复的 ID。因此,每次用户输入一个新数字时,您都必须重新开始迭代。
该List<Course> courselist
参数未使用。
话虽如此,该程序并未在逻辑上进行优化。该ifExists(,)
方法应仅适用于搜索具有相同 ID 的课程。至于处理用户输入,应该完全在方法之外完成。
这是该ifExists(,)
方法的示例
public static boolean ifExists(int id, Iterator<Course> iterator){
while (iterator.hasNext()) {
Course next = iterator.next();
if (next.id == id) return true;
}
return false;
}
然后在 main 方法中,您给用户的消息是基于此方法返回的值。这是一个例子:
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
while (ifExists(id)) {
System.out.println("Duplicated ID! Please try another number.");
id = scanner.nextInt();
} // If ifExists(id) returns false, continue to the code below to enter personal details
关于你的第二种方法
使用HashSet
代替List
or Iterator
。您可以直接调用HashSet.contains(Obj)
以检查Course
集合中是否已经存在,而无需遍历项目。即使List
也有这种方法,它也会遍历所有项目,这与您正在做的事情相似。
这是因为HashSet
按项目的哈希码而不是添加的顺序对项目进行排序,但List
不是。因此,当您调用该contains
方法时,它会查找条目号。(插入哈希码)。