我正在尝试创建一个函数,如果ArrayList中尚不存在该项,则应将其添加到ArrayList中。
这是我目前拥有的。
帐户类别:
public Account() {}
public Account(String accountName, int accountNumber, double accountBalance)
{
this.AccountName = accountName;
this.AccountNumber = accountNumber;
this.AccountBalance = accountBalance;
}
银行类别:
private ArrayList<Account> accountList;
public Bank() {
}
public boolean addAccount(Account account) {
if(!accountList.contains(account)) {
accountList.add(account);
return true;
}
return false;
}
测试器:
public class Main {
public static void main(String[] args){
Bank bank = new Bank("qLOwyONvKsM58ZdV& &yo", Bank.BranchLocations.values()[0]);
Account account = new Account("aucchQitgyzLV", 6329668, 479389.0);
System.out.println(bank.addAccount(account));
}
}
看起来我可以添加没有任何值的空帐户,但是如果给定3个值(名称,数字,余额)的可选参数,则它不接受。我不明白
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Account acc = (Account) obj;
return AccountBalance == acc.AccountBalance &&
AccountNumber == acc.AccountNumber &&
AccountName.equals(acc.AccountName);
}
答案 0 :(得分:1)
从ArrayList.contains(Object o)javadoc:
如果此列表包含指定的元素,则返回true。更多 形式上,当且仅当此列表包含至少一个时,才返回true 元素e使得(o == null?e == null:o.equals(e))。
这意味着,要使您的方法正常工作,您必须在Account类中实现hashcode / equals方法。
我为您的代码创建了一个模拟实现:
未定义哈希码/等于方法->使用对象类的默认哈希码/等于方法,以不同方式对待每个新对象。
import java.util.ArrayList;
import java.util.List;
public class Test{
static class Account {
private String AccountName;
private int AccountNumber;
private double AccountBalance;
public Account(String accountName, int accountNumber, double accountBalance)
{
this.AccountName = accountName;
this.AccountNumber = accountNumber;
this.AccountBalance = accountBalance;
}
public Account()
{
}
public String getAccountName() {
return AccountName;
}
public void setAccountName(String accountName) {
AccountName = accountName;
}
public int getAccountNumber() {
return AccountNumber;
}
public void setAccountNumber(int accountNumber) {
AccountNumber = accountNumber;
}
public double getAccountBalance() {
return AccountBalance;
}
public void setAccountBalance(double accountBalance) {
AccountBalance = accountBalance;
}
}
static class Bank {
private List<Account> accountList = new ArrayList<>();
private String inputStr = "";
private int inputInt = 0;
public Bank() {
}
public Bank(String string, int i) {
inputStr = string;
inputInt = i;
}
public boolean addAccount(Account account) {
if(!accountList.contains(account)) {
accountList.add(account);
return true;
}
return false;
}
}
public static void main(String[] args)
{
Bank bank = new Bank("qLOwyONvKsM58ZdV& &yo", 1);
Account account = new Account("aucchQitgyzLV", 6329668, 479389.0);
Account account5 = new Account("aucchQitgyzLV", 6329668, 479389.0);
Account account2 = new Account();
Account account3 = new Account();
Account account4 = new Account("sgdgrt", 0, 0.1);
Account account6 = new Account("sgdgrt", 0, 0.1);
System.out.println( "account added: "+bank.addAccount(account));
System.out.println("account5 added: "+bank.addAccount(account5));
System.out.println( "account2 added: "+bank.addAccount(account2));
System.out.println("account3 added: "+bank.addAccount(account3));
System.out.println("account4 added: "+bank.addAccount(account4));
System.out.println("account6 added: "+bank.addAccount(account6));
}
}
结果:
帐户已添加:true
account5已添加:true
account2已添加:true
account3已添加:true
account4已添加:true
account6已添加:true
显然不正确。
如果要在Account类中插入以下哈希码/等于方法:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(AccountBalance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((AccountName == null) ? 0 : AccountName.hashCode());
result = prime * result + AccountNumber;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (Double.doubleToLongBits(AccountBalance) != Double.doubleToLongBits(other.AccountBalance))
return false;
if (AccountName == null) {
if (other.AccountName != null)
return false;
} else if (!AccountName.equals(other.AccountName))
return false;
if (AccountNumber != other.AccountNumber)
return false;
return true;
}
结果将是:
添加的帐户:true
account5已添加:false
account2已添加:true
account3已添加:false
account4已添加:true
account6已添加:false
因此您的方法将正确运行。
答案 1 :(得分:0)
在对等值进行编辑后,您的代码必须能够按预期工作。
您可以list
来代替使用java.util.Set
并使用包含来检查重复项。它不允许重复。
如果要保留插入顺序,请改用LinkedHashSet
。它保留插入顺序并禁止重复。
您要做的就是重写equals和hashcode。
import java.util.LinkedHashSet;
import java.util.Set;
public class Bank {
private Set<Account> accountList= new LinkedHashSet<>();
public Bank() {
}
public boolean addAccount(Account account) {
if(!accountList.contains(account)) {
accountList.add(account);
return true;
}
return false;
}}
答案 2 :(得分:0)
我不知道发生了什么,但是我重新创建了您的程序,它可以按预期工作。问题可能出在您的银行分行逻辑上。我没有重写HashCode(),但这是一个很好的习惯。
public class BankingDemo {
public static void main(String[] args) {
Bank bank = new Bank();
Account account = new Account("aucchQitgyzLV", 6329668, 479389.0);
System.out.println(bank.addAccount(account));
System.out.println(bank.getList());
System.out.println(bank.addAccount(account));
System.out.println(bank.getList());
account = new Account("new account", 29228, 29202.2);
System.out.println(bank.addAccount(account));
System.out.println(bank.addAccount(account));
System.out.println(bank.getList());
}
}
class Account {
String AccountName;
int AccountNumber;
double AccountBalance;
public Account() {
}
public Account(String accountName, int accountNumber,
double accountBalance) {
this.AccountName = accountName;
this.AccountNumber = accountNumber;
this.AccountBalance = accountBalance;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Account acc = (Account) obj;
return AccountBalance == acc.AccountBalance
&& AccountNumber == acc.AccountNumber
&& AccountName.equals(acc.AccountName);
}
@Override
public String toString() {
return "{" + AccountName + "," + AccountNumber + "," + AccountBalance
+ "}";
}
}
class Bank {
private ArrayList<Account> accountList = new ArrayList<>();
public Bank() {
}
public boolean addAccount(Account account) {
if (!accountList.contains(account)) {
accountList.add(account);
return true;
}
return false;
}
public List<Account> getList() {
return accountList;
}
}