简体   繁体   English

如何在 ArrayList 中搜索元素? - Java

[英]How to search for elements in an ArrayList? - Java

I have created a To-Do-List program in java where you are able to add, remove, and view tasks with specific names, dates, times etc.我在 java 中创建了一个待办事项列表程序,您可以在其中添加、删除和查看具有特定名称、日期、时间等的任务。

However, I am trying to add an option where when the user selects "5", they are able to input a date, and if any tasks fall on that particular date, they will be listed.但是,我正在尝试添加一个选项,当用户选择“5”时,他们可以输入日期,如果有任何任务落在该特定日期,它们将被列出。

For example:例如:

----------------------
Main Menu
----------------------
1. Add a task
2. Delete a task
3. Delete all tasks
4. List all tasks
5. Search for a task
6. Exit the program

Enter choice: 5

Enter a date: 20/10/2020

Here are the tasks for 20/10/2020:

-task1-
-task2-
etc...

So far when I do this, no tasks are returning even if they do exist on that specific date.到目前为止,当我这样做时,即使在该特定日期确实存在任务,也不会返回任何任务。

Here is my code so far:到目前为止,这是我的代码:

public void searchTasks() throws ParseException {
System.out.println("Search for a task: ");
System.out.println("----------------------");
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a date (dd/mm/yyyy): ");

Scanner scanner = new Scanner(System.in);
String date = scanner.nextLine();

LocalDate theDate = LocalDate.parse(date, formatter);
String backToStr = formatter.format(theDate);

boolean found = false;
for (String task: currentList){
       String searched_date = task.split(", ")[1];
       if (searched_date.equals(backToStr)){
           found = true;
           System.out.println();
           System.out.println("----------------------");
           System.out.println("Here are the tasks on " + backToStr + ":");
           System.out.println("----------------------");
           System.out.println(task);
       }
}
if (!found){  // if there was no task found for the specified date
    System.out.println("No tasks found on this date");
}

}

When you are adding the theItem to currentList using currentList.add(theItem) , you are adding the all the information of the item (title, date, time, ...).当您使用currentList.add(theItem)theItem添加到currentList时,您正在添加该项目的所有信息(标题、日期、时间......)。

When you are searching for the task using contains(date) , you are only searching for the date.当您使用contains(date)搜索任务时,您只是在搜索日期。 So what happens behind the scene is that a date(eg, '20/10/2020') is being compared to something much longer (eg, 'myTitle, 20/10/2020, 10pm...') and they do not match, hence no task is shown.因此,幕后发生的事情是将日期(例如,'20/10/2020')与更长的时间(例如,'myTitle,20/10/2020,晚上 10 点......')进行比较,但他们没有匹配,因此不显示任务。

In addition to keyboardwarrior's answer, yes one way to do it would have been to have List<Tast> currentList as your ArrayList .除了键盘战士的回答之外,是的一种方法是将List<Tast> currentList作为您的ArrayList

However, if you're unwilling to change your code, you can workaround it by doing so:但是,如果您不愿意更改代码,则可以通过以下方式解决它:

1)Loop through the items(of type String ) in your currentList 1)循环通过您currentList中的项目(类型String
2)Split the item using the ", " as a separator, ie item.split(", ") 2)使用", "作为分隔符来拆分item,即item.split(", ")
3)During your conversion to String in getItem() , the date is the second thing you're adding to the string, ie the first thing after the first comma(,). 3) 在getItem()中转换为 String 期间,日期是您添加到字符串中的第二件事,即第一个逗号 (,) 之后的第一件事。
So item.split(", ")[1] is what's gonna contain the date.所以item.split(", ")[1]将包含日期。
4)Compare this to the date that you want to search, in every loop. 4)在每个循环中将此与您要搜索的日期进行比较。 Break when you find it.当你找到它时打破它。

boolean found = false;
for (String task: currentList){
       String searched_date = task.split(", ")[1];
       if searched_date.equals(date){
           found = true;
           System.out.println(task);
       }
}
if (!found){  // if there was no task found for the specified date
    System.out.println("No Task Found");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM