java - 在 Java 中查找和编辑 ArrayList
问题描述
我编写了一个代码来将用户提供的每个约会日历存储到一个 ArrayList 中。每个 ArrayList 包含标题、描述、日期和时间。
现在我想为用户提供一个选项来编辑他们选择的约会日历的数据。
我使用约会日历的标题进行比较。这是代码:
// Asks user to enter the Appointment title that they wish to edit
System.out.println("Please enter the appointment that you wish to edit: ");
String choicetitle = scan.next();
for (Appointment value : aplist)
{
// if the title of the Appointment matches with one of the data, then..
if (choicetitle.equalsIgnoreCase(value.title))
{
// ..the output occurs on the console and asks the user to enter the new data
input = new Scanner(System.in);
System.out.println("Please give the new data: \n");
System.out.println("Please give the date of the appointment! \n" +
"Year (format YYYY): ");
year = input.nextInt();
System.out.println("Month (format MM): ");
month = input.nextInt();
System.out.println("Day (format DD): ");
day = input.nextInt();
value.date = LocalDate.of(year, month, day);
System.out.println("Please give the time of the appointment! \n" +
"Hour (format hh): ");
hour = input.nextInt();
System.out.println("Minute (format mm): ");
min = input.nextInt();
value.time = LocalTime.of(hour, min);
System.out.println("Please give the title of the appointment: ");
value.title = input.next();
System.out.println("Please give the description of the appointment: ");
value.desc = input.next();
}
// if the user enters a title that doesn't exist
else
{
System.out.println("There's no appointment with this title!");
}
}
但是如果 ArrayList 中已经有 3 个元素(3 个约会日历)并且假设我要编辑第三个元素,它会给出以下输出:
There's no appointment with this title!
There's no appointment with this title!
Please give the new data:
Please give the date of the appointment!
Year (format YYYY):
有什么办法可以输出:
- 仅当用户给出的标题与 ArrayList 中的标题之一匹配时才给出扫描仪方法,
- 当用户输入的标题与 ArrayList 中的任何标题都不匹配时,只给出一个“此标题没有约会”?
提前致谢!
解决方案
看来您对调试代码的过程并不熟悉。所有计算机程序员都需要知道如何做到这一点。如果您使用的是IDE,那么它可能有一个调试器。你应该学习如何使用它。
您正在for
循环中进行所有处理。for
您应该首先通过循环找到请求的约会。循环终止后for
,只有当您找到了请求的约会时,您才应该处理它。这是一些帮助您入门的代码。
System.out.println("Please enter the appointment that you wish to edit: ");
String choicetitle = scan.next();
Appointment found = null;
for (Appointment value : aplist)
{
// if the title of the Appointment matches with one of the data, then..
if (choicetitle.equalsIgnoreCase(value.title))
{
found = value;
break;
}
}
if (value == null) {
System.out.println("There's no appointment with this title!");
}
else {
System.out.println("Please give the new data: \n");
System.out.println("Please give the date of the appointment! \n" +
"Year (format YYYY): ");
year = scan.nextInt(); // no need to create a separate `Scanner`
// rest of your processing code
}
或者,您可以使用流 API,尽管我认为您还没有了解这一点。下面的代码显示了如何使用流 API搜索特定约会。
aplist.stream()
.filter(value -> choicetitle.equalsIgnoreCase(value.title))
.findFirst();
推荐阅读
- php - 从两个不同的网页同时从 php 文件更新 SQL 数据库
- .net - MuleSoft 是否支持 .Net Core 应用程序
- amazon-web-services - 将 Snowflake 账户链接到 AWS 账户 ID
- python - 如何在 Python 中找到列表的最大值?
- python - 如何使用从字符串中抓取的一些运算符
- java - 在 Java 中反转 PriorityQueue 顺序的最快方法
- react-native - 在 react-native 的应用程序中处于睡眠模式或不活动状态后,模态不会打开
- reactjs - 传递整个对象时是否可以避免重新渲染所有输入?
- javascript - MongoDB 无法使用 findOneAndUpdate() 推送到数组
- ip-address - 您的服务器拥有静态 IP 地址有什么好处?