Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ArrayList中搜索元素?-JAVA_Java_Arraylist - Fatal编程技术网

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

如何在ArrayList中搜索元素?-JAVA,java,arraylist,Java,Arraylist,我用java创建了一个待办事项列表程序,您可以在其中添加、删除和查看具有特定名称、日期、时间等的任务 然而,我试图添加一个选项,当用户选择“5”时,他们能够输入一个日期,如果任何任务落在该特定日期,它们将被列出 例如: ---------------------- Main Menu ---------------------- 1. Add a task 2. Delete a task 3. Delete all tasks 4. List all tasks 5. Search for a

我用java创建了一个待办事项列表程序,您可以在其中添加、删除和查看具有特定名称、日期、时间等的任务

然而,我试图添加一个选项,当用户选择“5”时,他们能够输入一个日期,如果任何任务落在该特定日期,它们将被列出

例如:

----------------------
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...
到目前为止,当我这样做时,没有任务返回,即使它们确实存在于特定日期

以下是我目前的代码:

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");
}

}

当您使用
currentList.add(item)
item
添加到
currentList
时,您正在添加项目的所有信息(标题、日期、时间等)


使用
contains(date)
搜索任务时,仅搜索日期。因此,在幕后发生的事情是,一个日期(例如,“20/10/2020”)与更长的日期(例如,“myTitle,20/10/2020,10pm…”)进行比较,它们不匹配,因此没有显示任何任务。

除了keyboardwarrior的答案,是的,一种方法是将
列表currentList
作为
数组列表。

但是,如果您不愿意更改代码,可以这样做:

1) 循环浏览
当前列表中的项目(类型
字符串

2) 使用
“,”
作为分隔符拆分项目,即
项目。拆分(“,”

3) 在
getItem()
中转换为字符串的过程中,日期是要添加到字符串中的第二个内容,即第一个逗号(,)之后的第一个内容。
因此,
item.split(“,”[1]
将包含日期。
4) 在每个循环中,将其与要搜索的日期进行比较。当你找到它的时候就把它打破

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");
}

有什么方法可以搜索其中包含“20/10/2020”的任务吗?就我个人而言,我会在任务类型中包含currentList(因此列出currentList)。因此,当您将任务添加到currentList时,您将添加一个任务实例。这将使搜索更容易,因为您现在可以访问getDate()方法。现在,您可以使用
currentList[i].getDate().equals(data)
直接遍历currentList并比较日期。让我知道这是否有意义。你能更深入一点吗?我对Java很陌生,对不起。当我在ArrayList中将
String
更改为
Task
时,我在整个代码中都会遇到多个错误,因为ArrayList不再是类型
String
@DrMe。除了简单地更改ArrayList类型之外,您还需要进行其他更改。例如,您需要更改
showList()
方法,其中增强的
For
循环将迭代
Task
对象,而不是
String
对象。同样,您还需要在
addItem()
方法中更改添加项的方式。但是,当我更改行
字符串item=myTaskObj.getItem()时
to
Task-item=myTaskObj.getItem(),我收到一个错误,提示:
类型不匹配:无法从字符串转换为任务
。我已经更改了getItem()的返回类型,但是,这只会创建更多错误。请演示如何使用代码执行此操作?我对java非常陌生。@DrMe我已经编辑了我的答案,以便将其包含在代码中。我现在已经更新了问题中的代码,我现在如何实际显示该特定日期的任务列表?@DrMe您也许可以在那里添加一条SOP语句,我已经再次编辑了我的代码来实现这一点。注意:最初我认为,您只对第一个匹配任务感兴趣,所以我打破了循环。由于您需要所有匹配的任务,我删除了break语句。我更新了代码,这次使用
Date
而不是
String
作为任务的日期。然而,当我这样做时,我收到一个错误,它说:
不太可能是equals()的参数类型:Date似乎与字符串无关。