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

问题描述 投票:-1回答:3

这里是新手。在我尝试创建的这个特定程序中,我希望用户能够创建具有不同元素(字符串,整数,布尔值)的新对象,然后将其存储在ArrayList中,这与大多数Book / Library项目类似。为此,我创建了一个名为Game的类,在其中创建了构造函数,setters / getters和.toString方法。在mainmenu类中,我还创建了一些switch语句,以根据用户要执行的操作为用户提供不同的选择。在这里,我要求用户提供他们想要存储的新游戏的字段如前所述,我忘了提到我已经创建了一个名为存储的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函数不起作用,但就我所能达到的程度。

谢谢您的时间。

java object search arraylist element
3个回答
1
投票

我将假设storageGame对象列表的名称。在这种情况下,storage.equals()无法工作,因为您希望列表中的特定对象等于search_title,而不是整个列表。您可以通过执行以下操作来完成所需的操作:

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

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

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

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

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

然后只需通过键搜索地图:

Game found = storage.get(title);

0
投票
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.");
        }
    }

0
投票

假设您有以下游戏列表:

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);
© www.soinside.com 2019 - 2024. All rights reserved.