所以我必须为学校设计一个简单的银行程序,但我在检查数组中的特定帐号时遇到了问题。
基本信息,而非完整代码:
public class Account {
public String name;
int number;
double Balance;
private Account [] accounts;
public Account[] getAccounts() {
return accounts;
}
public boolean hasAccountNumber(int accountNumber) {
// How do I check my array of type Account if it has the passed in account number?
// I was trying to use the following:
Account[] b = getAccounts();
if(Arrays.asList(b).contains(accountNumber)){
return true;
} else{
return false;
}
}
}
为什么这不起作用?
最佳答案
您必须遍历 Account
并查询每个帐户的帐号。
boolean accountsContain(int accountNumber) {
for (Account account : getAccounts())
if (account.number == accountNumber)
return true;
return false;
}
(旁注:如果看起来应该有更简单的方法,那完全有,但 Java 还做不到。研究函数式编程)
关于java - (第一次编程)你如何检查一个类的数组的特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26185055/