【问题标题】:How to find object in arraylist with the Contains method如何使用 Contains 方法在数组列表中查找对象
【发布时间】:2019-07-29 12:48:26
【问题描述】:

您好,我有一个包含银行数据的数组类: 帐号、余额、账户类型和状态。 数组是用构造函数构建的:

Bank.accounts.add(new Account(this.accNumber, this.type, this.balance, this.status));

当我尝试找到一个数字(使用不同的方法)时,它没有找到。

该方法获取银行号码帐户的输入,然后将其与帐户数组列表进行比较。如果找到,则会打开一个控制台界面,但它永远不会找到帐号。 我尝试过扫描整数、字符串到整数,并在方法内部使用 contains 方法或作为独立方法(如下面的示例)使用,但没有成功。

方法是:

public void accountActions() {

    System.out.println("Enter number of account for more info");
    Boolean isFinished = false;
    while (!isFinished) {
        try {

            String scan = input.nextLine();
            Bank.number = Integer.parseInt(scan);
            isFinished = true;
        } catch (NumberFormatException e) {
            System.out.println("Please enter again\n------------------------------");
            isFinished = false;
        } catch (InputMismatchException e) {
            System.out.println("Please enter again\n------------------------------");
            isFinished = false;
        }
    }
    if (checkAccountNumber(number) == true) {
        System.out.println("=====================================");
        System.out.println("=                                   =");
        System.out.println("=   Enter command number            =");
        System.out.println("=                                   =");
        System.out.println("=   1. Withdraw money               =");
        System.out.println("=   2. Deposit                      =");
        System.out.println("=   3. Total money in all accounts  =");
        System.out.println("=                                   =");
        System.out.println("=====================================");

    } else {
        System.out.println(
                "No account was found by that number, would you like to try again?\n(((Yes/No)))\n---------------");
        yesNo("accountActions");
    }
}

而独立的方法是:

public boolean checkAccountNumber(int a) {
    boolean check = false;
    if (Bank.accounts.contains(a)) {
        check = true;
    }
    return check;
}

【问题讨论】:

  • 你应该避免在你的集合中使用原始类型。
  • 请注意,将Bank.accounts 数据结构建模为从帐号到Account 的哈希表可能是更好的设计。然后您可以简单地查找一个帐号。

标签: java arraylist collections contains


【解决方案1】:

由于您传递的是 int,而 Bank.accountsListAccount 对象,因此您的代码无法正常工作。

您可以使用Stream.anyMatch 来检查是否存在具有传入帐号的Account

anyMatch 相对于循环的主要优点是,它是短路的,它不会评估它将在第一次匹配时返回的流的所有元素。

根据anyMatchJava doc

如果没有必要,可能不会评估所有元素的谓词 确定结果。

示例代码:

class Bank {
    static List<Account> accounts;
    ...
}
public boolean checkAccountNumber(int a) {
    return Bank.accounts.stream().anyMatch(acc -> acc.accNumber.equals(a)); 
}

您可以通过使用Raw 类型来避免这个问题。将您的Bank.accounts 列表生成List&lt;Account&gt;,这样您就可以在编译时捕获此类错误。

【讨论】:

    【解决方案2】:

    您正在将帐户与 AccountNumber 进行比较。而不是这个,你应该这样做。

    public boolean checkAccountNumber(int a) {
    
        for (Account account of Bank.accounts){
            if (account.accNumber.equals(a)) {
                return true;
            }
        }
        return false;
    }
    

    【讨论】:

    • 另外,您必须在 Account 类中重写 'equals(...)' 和 'hashCode()' 方法
    • 没有必要,因为 AccountNumber 是整数,Mcaulish 正在尝试通过 AccountsNumber 查找 Accounts。
    • 为我工作!我使用 equals 而不是 contains,因为它是整数。我是一个初学者,并没有用我一直在寻找的东西来区分对象和原始之间的区别。非常感谢!
    猜你喜欢
    • 2014-12-18
    • 1970-01-01
    • 2021-08-28
    • 2023-04-04
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多