如何在arraylist(Java)中搜索对象的元素,如果它存在,则打印该对象的.toString

如何在arraylist(Java)中搜索对象的元素,如果它存在,则打印该对象的.toString,java,object,search,arraylist,element,Java,Object,Search,Arraylist,Element,在我试图创建的这个特定程序中,我希望用户能够使用不同的元素(字符串、int、布尔)创建一个新对象,然后将其存储在ArrayList中,这与大多数图书/图书馆项目类似。 为此,我创建了一个名为Game的类,其中创建了构造函数、setter/getter以及.toString方法。 在mainmenu类中,我还创建了一些switch语句,根据用户的需要为他们提供不同的选择。 在这里,我请求用户为他们想要存储的新游戏提供字段 正如前面指出的,我忘记提到我已经创建了一个名为storage的ArrayLi

在我试图创建的这个特定程序中,我希望用户能够使用不同的元素(字符串、int、布尔)创建一个新对象,然后将其存储在ArrayList中,这与大多数图书/图书馆项目类似。 为此,我创建了一个名为Game的类,其中创建了构造函数、setter/getter以及.toString方法。 在mainmenu类中,我还创建了一些switch语句,根据用户的需要为他们提供不同的选择。 在这里,我请求用户为他们想要存储的新游戏提供字段 正如前面指出的,我忘记提到我已经创建了一个名为storage的ArrayList

ArrayList存储=新的ArrayList();
private void gameInsert()
{
字符串标题;
字符串描述;
国际日期;
串质量;
给定布尔值;//(与在游戏类中创建的类似)
对于(int-index=0;index<1;index++)
{
System.out.println(“请输入标题:”);
title=键盘.nextLine().toLowerCase();
System.out.println(“请输入流派:”);
desc=键盘.nextLine();
System.out.println(“请输入发布年份:”);
date=Integer.parseInt(keyboard.nextLine());
System.out.println(“请输入产品质量”);
System.out.println(“NC=新条件,MC=新条件,BC=几乎未使用,U=已使用”);
质量=键盘.nextLine().toUpperCase();
System.out.println(“该项是否已发送给某人?”);
System.out.println(“按1=是|按2=否”);
if(Integer.parseInt(keyboard.nextLine())==1)
{
给定=真;
}
其他的
给定=假;
音量++;
添加(新游戏(标题、描述、日期、质量、给定));
} 
之后,我希望用户能够通过提供标题来搜索这个arrayList,并找到具有相同标题的游戏的所有可用信息

private void gameSearch() 
    {

        System.out.println("Enter the game's title!: ");
        String search_title = keyboard.nextLine().toLowerCase();
        for (int i= 0; i <storage.size(); i++) 
        if(storage.equals(search_title)) 
        {
            System.out.println("The game was found!");
            // print the .toString for this particular object;
            // or print by using the getters of the game class.
           // example given: title: ----
           //                genre: ---- etc.
        }
        else 
        {
            System.out.println("I am sorry, game not found. Please try again.");
        } 
private void gameSearch()
{
System.out.println(“输入游戏名称:”;
字符串搜索_title=keyboard.nextLine().toLowerCase();
对于(int i=0;i
private void gameSearch(){
布尔foundGame=false;
System.out.println(“输入游戏名称:”;
字符串搜索_title=keyboard.nextLine().toLowerCase();
对于(int i=0;i
我假设
存储
是给
游戏
对象列表的名称。如果是这样,
存储.equals()
无法工作,因为您希望列表中的特定对象与搜索标题相等,而不是整个列表。您可以通过执行以下操作来实现您的目标:

for (Game game:storage) {
  if game.getTitle().equals(search_title) {
    System.out.println("The game was found!");
  }
}
但是,如果我是你,我根本不会使用列表。我会使用地图。创建地图而不是列表:

Map<String, Game> storage = new HashMap<>();
然后只需按键搜索地图:

Game found = storage.get(title);

假设您有以下游戏列表:

List<Game> storage = List.of(
    new Game("My game title 1", "short description", 2019, "U", false),
    new Game("My game title 2", "short description", 2019, "U", false),
    new Game("My game title 3", "short description", 2019, "U", false),
    new Game("My game title 4", "short description", 2019, "U", false)
);
您的搜索功能是:

String result = storage.stream()
    .filter(game -> game.getTitle().equalsIgnoreCase(searchTitle))
    .map(Object::toString)
    .findFirst()
    .orElse("I am sorry, game not found. Please try again.");
System.out.println(result);

哦,我知道使用地图会容易得多。我对地图有一个基本的了解,但在这门课程中我们还没有被教过,因此我没有使用它。我想在那之后,我可以使用相同的功能,让用户可以搜索标题,然后使用setter对这个特定对象进行更改?的确,它确实有效!不幸的是,我们还没有学会如何使用过滤器/流。但很高兴看到有很多方法可以做到这一点:)
storage.put(title, new Game(title, desc, date, quality, given ));
Game found = storage.get(title);
List<Game> storage = List.of(
    new Game("My game title 1", "short description", 2019, "U", false),
    new Game("My game title 2", "short description", 2019, "U", false),
    new Game("My game title 3", "short description", 2019, "U", false),
    new Game("My game title 4", "short description", 2019, "U", false)
);
String searchTitle = "My game title 3";
String result = storage.stream()
    .filter(game -> game.getTitle().equalsIgnoreCase(searchTitle))
    .map(Object::toString)
    .findFirst()
    .orElse("I am sorry, game not found. Please try again.");
System.out.println(result);