[英]How to remove duplicates ArrayList from an ArrayList of ArrayList
[英]How to not add duplicates to an arrayList
如此简短的概述我的工作:我应该写一个使用我创建的帐户类的ATM类。 (account类包含以下方法:打开-创建一个新帐户,退出-退出当前帐户,登录-用户登录到他们的帐户,存款,取款,获取余额和终止-这会打破无限环)。 我的ATM类必须跟踪所有已打开的帐户,因此我想使用类型为account的数组列表。 但是由于某种原因,.contains()方法无法识别重复项,因此它会继续添加已经存在的帐户,而且我不确定如何解决此问题。 这是我的代码:THANKS =)
我的帐户类别:
导入java.util.ArrayList;
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
我的主要方法:
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());
}
}
}
代码仍在进行中,我还没有全部发布-这就是为什么括号没有全部关闭的原因。
ArrayList#contains
使用Object#equals
方法确定对象之间的相等性。 默认情况下,此简单操作比较对象的内存位置( o1 == o2
)
您将需要重写Account
类的equals
方法并比较(类似)帐户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
话虽如此,每次创建一个临时Account
您都会自动增加帐号,这并不是真正需要的。
更好的解决方案是使用某种类型的Map
(以帐号为键),以便您可以简单地执行以下操作...
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...
这确实意味着,每次创建一个Account
,都需要将其添加到Map
,因此可能需要某种工厂方法...
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类的重复项,为什么不创建一个唯一的标识符(主键)呢?
是来自数据库的数据还是只是静态输入而已? 如果它来自数据库,那么主键就可以了。 否则,只需将主键属性添加到模型中,然后检查是否有任何帐户具有相同的主键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.