gpt4 book ai didi

java - 检查数组列表是否有值

转载 作者:行者123 更新时间:2023-12-01 23:32:53 25 4
gpt4 key购买 nike

我正在尝试解决我的问题,我必须查看在将值输入到数组列表中时是否已添加该值,如果是,我想沿着名称行显示一条消息已经存在

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;
}
}
}

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

最佳答案

您可以通过两种方式做到这一点:

  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);
    }

如果您要使用后者,最好使用 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 });
}
}

关于java - 检查数组列表是否有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19081211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com