Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我正在尝试用compareTo方法对数组列表进行排序_Java_Arraylist_Compareto - Fatal编程技术网

Java 我正在尝试用compareTo方法对数组列表进行排序

Java 我正在尝试用compareTo方法对数组列表进行排序,java,arraylist,compareto,Java,Arraylist,Compareto,我正在尝试对银行账户进行排序,它首先按名称的字母顺序排序,其次按账户中的金额从大到小排序。不幸的是,CompareTo方法似乎无法正常工作,唯一有效的部分是第二部分,它在第二部分中按数量排序 银行账户类别 /** * A bank account has a balance, the name of the account holder, * and an account number. The balance can be changed by deposits * and withdr

我正在尝试对银行账户进行排序,它首先按名称的字母顺序排序,其次按账户中的金额从大到小排序。不幸的是,CompareTo方法似乎无法正常工作,唯一有效的部分是第二部分,它在第二部分中按数量排序

银行账户类别

/**
 * 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;
}
}
/**
*银行账户有余额,账户持有人的姓名,
*和一个帐号。余额可以通过存款来改变
*和提款。
*/
公共类BankAccount实现可比性{
/**
*构造一个余额为零的银行帐户。
*@param name帐户持有人的姓名
*/
公共银行帐户(字符串名称){
this.name=名称;
余额=0;
accountNo=++lastAccountNo;
}
/**
*构造具有给定余额的银行帐户。
*@param initialBalance初始余额
*@param name帐户持有人的姓名
*/
公共银行帐户(字符串名称,双首字母余额){
this.name=名称;
平衡=初始平衡;
accountNo=++lastAccountNo;
}
/**
*把钱存入银行帐户。
*@param amount要存入的金额
*/
公共作废保证金(双倍金额){
双新余额=余额+金额;
平衡=新平衡;
}
/**
*从银行帐户取款。
*@param amount要提取的金额
*/
公开作废取款(双倍金额){
双新余额=余额-金额;
平衡=新平衡;
}
/**
*获取银行帐户的当前余额。
*@返回当前余额
*/
公共双getBalance(){
收益余额;
}
/**
*获取帐户持有人的名称。
*@返回帐户持有人的姓名
*/
公共字符串getName(){
返回名称;
}
/**
*获取该帐户的帐户号。
*@返回该帐户的帐号
*/
public int getAccountNo(){
返回帐号;
}
/**
*返回银行帐户的字符串表示形式。格式为
*是“名称:账户无余额”
*@返回银行帐户的字符串表示形式。
*/
公共字符串toString(){
返回名称+”:账号:“+AccountNo+”余额:“+balance;
}
私人双平衡;
私有字符串名称;
私人国际帐户号码;
私有静态int lastAccountNo=0;
公共国际比较(银行账户b){
if(this.name.compareTo(b.name)==0&&this.balance>b.balance)返回0;
else if(this.name.compareTo(b.name)<0&&this.balance0&&this.balance==b.balance)返回-1;
//else if(this.name.compareTo(b.name)==0)返回0;
//else if(this.name.compareTo(b.name)<0)返回1;
//如果(this.name.compareTo(b.name)>0)返回-1;
否则如果(this.balance==b.balance)返回0;
否则如果(this.balanceb.balance)返回-1;
否则返回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);
        }
    }
}
import java.util.ArrayList;
导入java.util.Collections;
银行帐户测试员{
公共静态void main(字符串参数[]){
ArrayList accounts=新的ArrayList();
新增(新银行账户(“Louis Oliphant”,100.0));
增加(新银行账户(“Louis Oliphant”,100.10));
新增(新银行账户(“Louis Oliphant”,100.0));
添加(新银行账户(“Jane Doe”,100.0));
添加(新银行账户(“Jane Doe”,99.0));
添加(新银行账户(“Jane Doe”,100.0));
System.out.println(“******未排序*******”);
用于(银行账户b:账户){
系统输出打印ln(b);
}
System.out.println();
收款.分类(账户);
System.out.println(“*******已排序*******”);
用于(银行账户b:账户){
系统输出打印ln(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()));
}
public int比较(银行账户o1、银行账户o2){
如果(o1.getName().compareTo(o2.getName())<0){return-1;}
如果(o1.getName().compareTo(o2.getName())>0){return 1;}
返回值(0-Double.compare(o1.getBalance(),o2.getBalance());
}
正如其他人提到的,我建议使用一个分开的
比较器作为
sort()
方法的参数,而不是实现接口
Comparable
compareTo()
方法。实现
compareTo()
定义对象的“自然”顺序。您的排序看起来像是特定于该情况的排序,而不是自然排序。看

顺便说一句,在处理货币值时,应该使用
BigDecimal
s而不是
double
s。看


注1:当帐户名相等时,上面的代码块返回字典的差异,请参见,也就是说,它们并不总是返回
-1
0
1


注2:当您计划同时重写
equals(Object)
hashCode()
方法时,请记住强烈建议使用
x.compareTo(y)==0
等于
x.equals(y)
,但这并不是严格要求
x.compareTo(y)
,请参阅。

当两个对象的余额和名称相同时,不需要返回0吗?分两步返回-1进行比较。首先,比较名称,然后比较余额。对于名称比较的每个可能结果(等于、小于、大于),都有相等数量的余额比较。如果你再看一遍你的代码,你并没有涵盖所有的可能性。另一方面,由于舍入错误,您不会使用double来表示货币。最好使用BigDecimal。在名称相等加上3个可能的余额的情况下编写代码。然后转到下一个案例。使用比较器。我支持使用(外部)
comparia