检查arraylist是否有值

时间:2013-09-29 17:44:06

标签: java arrays add

我正在尝试解决我的问题,我必须查看是否在向arraylist中输入此值时已经添加了这个值,如果是这样,我想在名称行中显示消息已经存在

import java.util.*;

class Bank{

void command(){
 CommandReader commandReader=new CommandReader(this);
 commandReader.run();
}
ArrayList <Account> accounts=new ArrayList <Account>();

public static void main(String[] args){
 new Bank().command();

}
void print(String name){

System.out.println("Account of "+name);
int b=0;
for(int i=0; i<accounts.size(); i++){ 
  b=accounts.get(i).balance(name);
  break;
  } 
System.out.println("Balance: "+b);//dit moet naar account verwjizen op een of andere      manier zodat hij weet waar hij balance() moet zoeken
 }
void printAll(){

/*
for(int i =0; i<accounts.size()-1;i++){
 print(accounts[i].name);//klopt niet, weet niet wat er wel moet staan
}*/
}
void enroll(String name){
Account account=new Account(name,0);
accounts.add(account);
}
void deposit(String name, int amount){
for(int i=0; i<accounts.size(); i++){
  accounts.get(i).deposit(amount, name);
  } 
}


 void withdraw(String name, int amount){//hiervoor moet je denk ik een methode in account maken zodat e met balance kan rekenen?
for(int i=0; i<accounts.size(); i++){
  int x;
  x=accounts.get(i).balance(name);
  if (x>-1000){
  amount = amount*-1;
   accounts.get(i).deposit(amount, name);

  } 
}
}
  void printRed(){

}
void interest(double rate){
for(int i=0; i<accounts.size(); i++){
  accounts.get(i).inter(rate);
}
 }   

}

class Account{
String name;
int balance;

void inter(double rate){
 balance+=rate
   /100*balance;
}

Account(String n, int b){//weet niet of dit nodig is, denk het wel
 this.name=n;
 this.balance=b;
}
String name(){
  return this.name;      
} 
int balance(String name){
  if(name.equals(this.name)){
    return this.balance;
  }
  return 0;
}
void deposit(int b, String name){
  if(name.equals(this.name)){
  this.balance+=b;
  }
}
 }

是我通过方法将值添加到数组中的方法。(零可以忽略)

2 个答案:

答案 0 :(得分:3)

您可以通过两种方式实现:

  1. 自己写一个比较。然后遍历所有数组元素以验证对象是否已存在。

  2. 让您的Account类覆盖equalshashCode方法,然后使用List#contains验证对象是否已存在于列表中。

    一个简短的例子:

    Account account = new Account(name,0);
    //this assumes that Account class has already overridden equals and hashCode methods
    if (!accounts.contains(account)) {
        accounts.add(account);
    }
    
  3. 如果您打算使用后者,最好使用Set而不是List,因为Set已经处理了该对象在集合中应该是唯一的


    在编辑中,当前错误是Account必须覆盖equalshashCode方法。执行此操作时,代码将按预期运行。

    为两者提供基本实施:

    class Account {
        String name;
        int balance;
    
        void inter(double rate) {
            balance+=rate
           /100*balance;
        }
    
        Account(String n, int b) {
            this.name=n;
            this.balance=b;
        }
        String name() {
            return this.name;
        }
        int balance(String name) {
            if(name.equals(this.name)) {
                return this.balance;
            }
            return 0;
        }
        void deposit(int b, String name){
            if(name.equals(this.name)){
                this.balance+=b;
            }
        }
        //from here, this is the code you need
        @Override
        public boolean equals(Object o) {
            if (o == this) {
                return true;
            }
            if (o instanceof Account) {
                Account another = (Account)o;
                return this.name.equals(another.name);
            }
            return false;
        }
        @Override
        public int hashCode() {
            //if working with Java 7, uncomment below LoC
            //return java.util.Objects.hash(name, balance);
            //if using Java SE 6 or less
            return java.util.Arrays.hashCode(new Object[]{ name, balance });
        }
    }
    

答案 1 :(得分:0)

覆盖equals()hashcode(),改用Setadd()返回boolean,具体取决于是否忽略了