如何在数组列表 (Java) 中搜索 object 的元素,如果存在,则打印 object 的 .toString

How to search an element of an object in an arraylist (Java) and if it exists, print the .toString of that object

在我尝试创建的这个特定程序中,我希望用户能够创建一个包含不同元素(字符串、整数、布尔值)的新 object,然后将其存储在 ArrayList 中,类似于大多数 Book/Library 项目。 为此,我创建了一个名为 Game 的 class,我在其中创建了一个构造函数 setters/getters 以及一个 .toString 方法。 在主菜单 class 中,我还创建了一些 switch 语句,以根据用户想要执行的操作为用户提供不同的选择。 在这里我要求用户提供他们想要存储的新游戏的字段 正如指出的那样,我忘了提及我创建了一个名为 storage

的 ArrayList
ArrayList <Game> storage = new ArrayList <Game> ();
private void gameInsert() 
    {
        String title;
        String desc;
        int date;
        String quality;
        Boolean given;  //(similar to the ones created in the game class)

        for (int index = 0; index < 1; index++) 
        {
        System.out.println("Please enter a title: ");
        title = keyboard.nextLine().toLowerCase();
        System.out.println("Please enter the genre: ");
        desc = keyboard.nextLine();
        System.out.println("Please enter the year of release: ");
        date = Integer.parseInt(keyboard.nextLine());
        System.out.println("Please enter the quality of the product");
        System.out.println("NC = new condition, MC= Mint condition, BC = Barely Used , U = Used ");
        quality = keyboard.nextLine().toUpperCase();
        System.out.println("Is the item borrwed to someone? ");
        System.out.println("Press 1 = Yes | Press 2 = No");
        if ( Integer.parseInt(keyboard.nextLine()) == 1) 
        {
            given = true;
        }
        else
            given = false;
        volume ++;
        storage.add(new Game(title, desc, date, quality, given ));
        } 

之后,我希望用户能够通过提供标题来搜索此 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.");
        } 

我知道我的 gameSearch 功能不起作用,但这是我所能得到的。

private void gameSearch() {
        boolean foundGame = false;
        System.out.println("Enter the game's title!: ");
        String search_title = keyboard.nextLine().toLowerCase();
        for (int i = 0; i < storage.size(); i++) {
            Storage storage = storage.get(i);
            if (storage.getTitle().equalsIgnoreCase(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.
                foundGame = true;
                break;
            }
        }
        if(!foundGame){
            System.out.println("I am sorry, game not found. Please try again.");
        }
    }

我假设 storageGame 对象列表的名称。如果是这种情况,storage.equals() 将无法工作,因为您希望列表中的特定对象等于 search_title,而不是整个列表。你可以通过这样做来完成你想要的:

for (Game game:storage) {
  if game.getTitle().equals(search_title) {
    System.out.println("The game was found!");
  }
}

但是,如果我是你,我根本不会使用列表。我会改用地图。创建地图而不是列表:

Map<String, Game> storage = new HashMap<>();

放入地图而不是添加到列表中:

storage.put(title, new Game(title, desc, date, quality, given ));

然后通过key搜索地图:

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

而您正在寻找 My game title 3:

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