gpt4 book ai didi

java - 我尝试使用 CompareTo 方法对数组列表进行排序

转载 作者:行者123 更新时间:2023-12-01 13:15:58 26 4
gpt4 key购买 nike

我正在尝试对银行帐户进行排序,首先按名称字母顺序排序,然后按帐户中的金额从多到少排序。不幸的是,CompareTo 方法似乎无法正常工作,唯一有效的部分是按金额排序的第二部分。

BankAccount 类

/**
* A bank account has a balance, the name of the account holder,
* and an account number. The balance can be changed by deposits
* and withdrawals.
*/
public class BankAccount implements Comparable<BankAccount> {

/**
* Constructs a bank account with a zero balance.
* @param name the name of the account holder
*/
public BankAccount(String name) {
this.name = name;
balance = 0;
accountNo = ++lastAccountNo;
}

/**
* Constructs a bank account with a given balance.
* @param initialBalance the initial balance
* @param name the name of the account holder
*/
public BankAccount(String name, double initialBalance) {
this.name = name;
balance = initialBalance;
accountNo = ++lastAccountNo;
}

/**
* Deposits money into the bank account.
* @param amount the amount to deposit
*/
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}

/**
* Withdraws money from the bank account.
* @param amount the amount to withdraw
*/
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}

/**
* Gets the current balance of the bank account.
* @return the current balance
*/
public double getBalance() {
return balance;
}

/**
* Gets the name of the account holder.
* @returns the name of the account holder
*/
public String getName() {
return name;
}

/**
* Gets the account number of the account.
* @returns the account number of the account
*/
public int getAccountNo() {
return accountNo;
}

/**
* Returns a String representation of the BankAccount. The format
* is "name: accountNo balance"
* @returns a String representation of the BankAccount.
*/
public String toString() {
return name + ": AccountNo:" + accountNo + " balance:" + balance;
}

private double balance;
private String name;
private int accountNo;
private static int lastAccountNo=0;

public int compareTo(BankAccount b) {

if(this.name.compareTo(b.name) == 0 && this.balance > b.balance) return 0;
else if(this.name.compareTo(b.name) < 0 && this.balance < b.balance) return 1;
else if(this.name.compareTo(b.name) > 0 && this.balance == b.balance) return -1;

//else if(this.name.compareTo(b.name) == 0) return 0;
//else if(this.name.compareTo(b.name) < 0) return 1;
//else if(this.name.compareTo(b.name) > 0) return -1;
else if(this.balance == b.balance) return 0;
else if(this.balance < b.balance) return 1;
else if(this.balance > b.balance) return -1;
else return 0;
}
}

银行账户测试器

import java.util.ArrayList;
import java.util.Collections;

class BankAccountTester {
public static void main(String args[]) {
ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
accounts.add(new BankAccount("Louis Oliphant", 100.0));
accounts.add(new BankAccount("Louis Oliphant", 100.10));
accounts.add(new BankAccount("Louis Oliphant", 100.0));
accounts.add(new BankAccount("Jane Doe", 100.0));
accounts.add(new BankAccount("Jane Doe", 99.0));
accounts.add(new BankAccount("Jane Doe", 100.0));
System.out.println("*****Unsorted******");
for (BankAccount b : accounts) {
System.out.println(b);
}
System.out.println();
Collections.sort(accounts);
System.out.println("******Sorted******");
for (BankAccount b : accounts) {
System.out.println(b);
}
}
}

最佳答案

你应该重新考虑你的逻辑。如果两个银行账户的名字不一样,那么金额就没有必要比较了,不是吗?

public int compare(BankAccount o1, BankAccount o2) {
int compareName = o1.getName().compareTo(o2.getName());
if (compareName == 0) {

// When the balance of the current amount is greater than the
// balance of the compared account, then the current accounts
// "precedes" the compared account, and thus -1 is returned.
// If the balance is less, than this account "follows" the
// compared account, and 1 is preceded.
// Otherwise, 0 is returned.
return (0 - Double.compare(o1.getBalance(), o2.getBalance()));
}
else {
return compareName;
}
}

或更短:

public int compare(BankAccount o1, BankAccount o2) {
if (o1.getName().compareTo(o2.getName()) < 0) { return -1; }
else if (o1.getName().compareTo(o2.getName()) > 0) { return 1; }
return (0 - Double.compare(o1.getBalance(), o2.getBalance()));
}

正如其他人提到的,我建议使用分开 Comparator<T>作为 sort() 的参数方法,而不是实现 compareTo()接口(interface)方法Comparable 。实现compareTo()定义对象的“自然”顺序。您的排序看起来像是特定于该情况的排序,而不是自然排序。请参阅Java : Comparable vs Comparator .

顺便说一句,在处理货币值时,您应该使用 BigDecimal s 而不是 double s。请参阅Double vs. BigDecimal? .

<小时/>

注1:上面的代码块返回账户名相等时的字典序差异,参见 String.compareTo(String) ,也就是说,它们并不总是返回 -1 , 01 .

注释 2:当您还计划覆盖 equals(Object) 时和hashCode()方法,请记住,尽管不是严格要求x.compareTo(y) == 0,但强烈建议使用它。等于x.equals(y) ,参见 Comparable<T>.compareTo(T object) .

关于java - 我尝试使用 CompareTo 方法对数组列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22468421/

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