Java 如何不向arrayList添加重复项
我的任务概述如下: 我应该编写一个ATM类,它使用我创建的accounts类。(accounts类包含方法,open-创建新帐户,quit-退出当前帐户,login-用户登录其帐户,存款,取款,获取余额,终止-这打破了无限循环)。我的ATM类必须跟踪所有打开的帐户,所以我想使用account类型的数组列表。但是由于某种原因,.contains()方法无法识别重复项,因此它不断添加已经存在的帐户,我不太确定如何解决此问题。这是我的密码: 谢谢=) 我的帐户类别: 导入java.util.ArrayListJava 如何不向arrayList添加重复项,java,oop,arraylist,duplicates,contains,Java,Oop,Arraylist,Duplicates,Contains,我的任务概述如下: 我应该编写一个ATM类,它使用我创建的accounts类。(accounts类包含方法,open-创建新帐户,quit-退出当前帐户,login-用户登录其帐户,存款,取款,获取余额,终止-这打破了无限循环)。我的ATM类必须跟踪所有打开的帐户,所以我想使用account类型的数组列表。但是由于某种原因,.contains()方法无法识别重复项,因此它不断添加已经存在的帐户,我不太确定如何解决此问题。这是我的密码: 谢谢=) 我的帐户类别: 导入java.util.Array
public class Account
{
public ArrayList<Account> accounts;
public static int NextAcctNo = 999;
private int accountNo;
private double balance;
public Account()
{ // constructor
balance = 0.0;
accountNo = ++NextAcctNo;
}
public void Open()
{
System.out.println("New account number: " + accountNo);
}
public void Quit()
{
System.out.println("Goodbye");
}
public void Login(int accountNo)
{
System.out.println("Hello");
this.accountNo = accountNo;
}
public void Deposit(double amount)
{ // method
balance += amount;
System.out.println("Deposited: " + amount);
}
public void Withdraw(double amount)
{ // method
balance -= amount;
System.out.println("Withdrew: " + amount);
}
public double Balance()
{ // method
System.out.println("Balance: " + balance);
return balance;
}
public void Terminate()
{
System.out.println("Terminated");
}
public int getAccountNo()
{ // method
return accountNo;
}
}//class Account
公共类帐户
{
公共ArrayList账户;
公共静态int NextAcctNo=999;
私人国际帐户号码;
私人双平衡;
公共帐户()
{//构造函数
余额=0.0;
accountNo=++nextactno;
}
公开作废
{
System.out.println(“新账号:+accountNo”);
}
公开作废退出()
{
System.out.println(“再见”);
}
公共无效登录(int accountNo)
{
System.out.println(“你好”);
this.accountNo=accountNo;
}
公共作废保证金(双倍金额)
{//方法
余额+=金额;
系统输出打印项次(“存入:+金额);
}
公开作废取款(双倍金额)
{//方法
余额-=金额;
系统输出打印项次(“提取:+金额);
}
公共双平衡()
{//方法
系统输出打印项次(“余额:+余额);
收益余额;
}
公共无效终止()
{
系统输出打印项次(“终止”);
}
public int getAccountNo()
{//方法
返回帐号;
}
}//类别帐户
我的主要方法是:
import java.util.Scanner;
import java.util.ArrayList;
public class ATM
{
// search for accounts for D, W, B
public static void main(String [] args)
{
ArrayList<Account> accounts = new ArrayList<Account>();
int counter = 0;
while (true)
{
Scanner commandScanner = new Scanner(System.in);
System.out.print("Enter a command - O, Q, L, D, W, B, T: ");
String command = commandScanner.nextLine();
if (command.equals("O") == true)
{
Account newAccount = new Account();
accounts.add(newAccount);
counter++;
newAccount.Open();
}
else if (command.equals("Q") == true)
{
if (accounts.size() == 0)
{
System.out.println("Error");
}
else
{
accounts.get(counter-1).Quit();
}
}
else if (command.equals("L") == true)
{
Scanner numberScanner = new Scanner(System.in);
System.out.println("Enter your account number: ");
int accountNo = numberScanner.nextInt();
Account temp = new Account();
temp.accountNo = accountNo;
if (counter == 0)
{
accounts.add(temp);
counter++;
}
else if (counter > 0)
{
if (accounts.contains(temp.accountNo) == true)
{
counter+=0;
}
else
{
Account newAccount = temp;
newAccount.Login(accountNo);
accounts.add(newAccount);
counter++;
System.out.println(accounts.size());
}
}
}
import java.util.Scanner;
导入java.util.ArrayList;
公共级自动取款机
{
//搜索D、W、B的帐户
公共静态void main(字符串[]args)
{
ArrayList accounts=新的ArrayList();
int计数器=0;
while(true)
{
Scanner commandScanner=新扫描仪(System.in);
打印(“输入命令-O、Q、L、D、W、B、T:”);
String命令=commandScanner.nextLine();
if(command.equals(“O”)==true)
{
账户newAccount=新账户();
账户。添加(新账户);
计数器++;
newAccount.Open();
}
else if(command.equals(“Q”)==true)
{
如果(accounts.size()==0)
{
System.out.println(“错误”);
}
其他的
{
accounts.get(counter-1.Quit();
}
}
else if(command.equals(“L”)==true)
{
扫描仪编号扫描仪=新扫描仪(System.in);
System.out.println(“输入您的帐号:”);
int accountNo=numberScanner.nextInt();
账户温度=新账户();
temp.accountNo=accountNo;
如果(计数器==0)
{
账户。添加(临时);
计数器++;
}
否则如果(计数器>0)
{
if(accounts.contains(temp.accountNo)=true)
{
计数器+=0;
}
其他的
{
账户newAccount=temp;
newAccount.Login(accountNo);
账户。添加(新账户);
计数器++;
System.out.println(accounts.size());
}
}
}
代码仍在编写中,我还没有全部发布-这就是为什么括号没有全部关闭的原因。可能刚刚超出我的理解范围,但是如果您正在寻找帐户类的副本,为什么不在创建时添加唯一标识符(主键) 数据是来自数据库还是只是静态输入?如果是来自数据库,则主键可以。否则,只需向模型中添加主键属性,然后检查是否有任何帐户具有相同的主键。
ArrayList#contains
使用对象#equals
方法确定相等性在对象之间。默认情况下,此简单操作会比较对象的内存位置(o1==o2
)
您需要重写
Account
类的equals
方法,并比较(类似)Account id
public class Account {
//...
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Account other = (Account) obj;
if (this.accountNo != other.accountNo) {
return false;
}
return true;
}
}//class Account
现在,说到这里,每次创建一个临时的帐户时,您都会自动增加帐号,这并不是真正需要的
更好的解决方案是使用某种类型的地图
,键入账号,这样您就可以简单地执行以下操作
Scanner numberScanner = new Scanner(System.in);
System.out.println("Enter your account number: ");
int accountNo = numberScanner.nextInt();
Account account = accountsMap.get(accountNo);
// Check for a null return result...
这意味着,每次创建帐户时,都需要将其添加到映射中,因此可能需要调用某种工厂方法
ps
equals和hashCode
方法之间存在契约性联系,这通常意味着,当您重写一个方法时,您应该重写另一个方法
请注意,每当重写hashCode方法时,通常都需要重写该方法,以便维护hashCode方法的一般约定,该约定规定相等的对象必须具有相等的哈希代码
例如
public class Account {
//...
@Override
public int hashCode() {
int hash = 5;
hash = 43 * hash + this.accountNo;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Account other = (Account) obj;
if (this.accountNo != other.accountNo) {
return false;
}
return true;
}
}//class Account
您需要重写Account
类的hashcode
和equals
方法,以便ArrayList
检测对象的重复实例,您可以使用“HashSet”概念来存储值。重写hashcode和equals后,您可以使用set作为