gpt4 book ai didi

java - 无法从 ArrayList 中找到项目?

转载 作者:行者123 更新时间:2023-12-02 06:24:15 26 4
gpt4 key购买 nike

我似乎无法将我保存的帐户存储在我的 ArrayList 中。我认为最新的会覆盖最旧的,但我需要它们全部堆叠在 ArrayList 中。

当我运行该程序时,我创建(添加到 ArrayList)一个新帐户,Greg 说。然后我继续使用该帐户,稍后添加另一个帐户,鲍勃说。然后我点击“搜索帐户”并输入 Greg,它没有找到它,但它找到了 Bob。然而,如果我在创建 Bob 之前搜索 Greg,它就会找到它!

以下是我的代码的相关部分:

////////// DECLARATIONS /////////
public static ArrayList dataStore;
public static int index;
dataStore = new ArrayList(10);
index = 0;

/////////////////////////////// ADD NEW ACCOUNT ////////////////////////////////
else if(source.equals("Add new account")) {
//************* Output
accountName = JOptionPane.showInputDialog("Enter the account name: ");
account.setName(accountName);

String strInitialBalance = JOptionPane.showInputDialog("Enter your initial
balance: ");
initialBalance = Double.parseDouble(strInitialBalance);
account.setBalance(initialBalance);

account = new CheckingAccount(initialBalance, accountName);
dataStore.add(index++, account);

}

//////////////////////////////// FIND ACCOUNT /////////////////////////////////
else if(source.equals("Find an account")) {
String str, name;

str = JOptionPane.showInputDialog("Enter the Account name: ");

for (int index = 0; index < dataStore.size(); index++) {
Account datum = (Account)dataStore.get(index);

if (str.equals(datum.getName())) {
name = datum.getName();
textArea.setText("Found Account for " + name);
}
else
textArea.setText("No Accounts found!");

} // for

}

他们所指的这个“帐户”对象是我存储所有数据的一个类。这是它的标题:

public class CheckingAccount extends Account implements Serializable {
// ...
// Methods
// ...
}

最佳答案

这是您的原始代码块。

for (int index = 0; index < dataStore.size(); index++) {
Account datum = (Account)dataStore.get(index);

if (str.equals(datum.getName())) {
name = datum.getName();
textArea.setText("Found Account for " + name);
}
else
textArea.setText("No Accounts found!");

}

我认为在 for 循环中使用 else 语句是没有意义的,请尝试下面的代码...

boolean found = false;
for (int index = 0; index < dataStore.size(); index++) {
Account datum = (Account)dataStore.get(index);

if (str.equals(datum.getName())) {
name = datum.getName();
textArea.setText("Found Account for " + name);
found = true;
}
}
if(!found){

textArea.setText("No Accounts found!");
}

我认为你不想要 else{}里面for(...) loop因为当您浏览数据结构时,很多时候,您的初始 if (str.equals(datum.getName()))声明将失败,因为您尚未找到该帐户。但是,如果您最终找到它,您将遇到 if(...)条件并通过 textArea 告诉用户您已找到该帐户。然后你设置一个boolean确实你找到了它。如果你遍历 ENTIRE 数据结构但没有找到它, boolean 值仍然等于 false,并且你将满足第二个 if 的条件,即 if(!found)在for循环之后,会告诉用户你已经搜索了整个数据结构,还没有找到该账户。

另外,我认为你的第一个 else if 应该看起来更像下面的代码(我不知道 CheckingAccount 的方法签名是什么样的,所以其中一些是对实际语法的猜测)

else if(source.equals("Add new account")) {
//************* Output
//use constructor below if you have a default constructor aka no paramaters
CheckingAccount account = new CheckingAccount();
//now that you have object linked to account, you can use setters
accountName = JOptionPane.showInputDialog("Enter the account name: ");
//set the name
account.setName(accountName);

String strInitialBalance = JOptionPane.showInputDialog("Enter your initial
balance: ");
initialBalance = Double.parseDouble(strInitialBalance);
//set the balance
account.setBalance(initialBalance);

//dont make a new object now as you did before..just add the object to your arraylist
dataStore.add(index++, account);

}

关于java - 无法从 ArrayList 中找到项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20715537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com