优先队列 java

PriorityQueue java

我正在写一个优先级队列class,我想根据账户余额进行排序和打印。它打印值,但问题是它打印传递给构造函数的参数的十六进制值。我在代码中哪里出错了?

账户:

public class Account implements Comparable<Account> {

    private String firstName;
    private String lastName;
    private double balance;
    private int accountNumber;

    public Account(String firstName, String lastName, double balance, int accountNumber){

        this.firstName = firstName;
        this.lastName = lastName;
        this.balance = balance;
        this.accountNumber = accountNumber;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(int accountNumber) {
        this.accountNumber = accountNumber;
    }
    public boolean equals(Account x){
        return firstName.equals(x.firstName);
    }

    @Override
    public int compareTo(Account o) {

        return(int) (this.balance - o.balance);

       // Account other = (Account)o;

        /*if(balance<other.balance)
            return -1;
        if(balance==other.balance)
            return 0;
        return 1;*/

       /* int c = this.firstName.compareTo(o.firstName);

        if(c < 0){
            return -1;
        }else if(c == 0){
            if(this.balance < 0 && o.balance < 0){
                if(this.balance < o.balance){
                    return 1;
                }
            }

        }
        return 1;*/


    }

}

帐户应用程序:

package account;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;

/**
 *
 * @author saner20
 */
public class AccountApp {

    public static void main(String []args){

        Account account1 = new Account("billy", "bob", 10.00, 1);
        Account account2 = new Account("tom","sawyer", 20.00, 2);
        //Account account3 = new Account("bob","builder", 30, 3);

        PriorityQueue<Account> account = new PriorityQueue<>();

        account.offer(account1);
        account.add(account2);
        //account.add(account3);

        while(!account.isEmpty())
        {
            System.out.println("Print queue: " + account.remove());
            //time.remove();
        }
        //Arrays.sort(account.toArray());
    }
}

覆盖 Account class 的 toString() 方法。

类似于:

public class Account implements Comparable<Account> {

    private String firstName;
    private String lastName;
    private double balance;
    private int accountNumber;

    public Account(String firstName, String lastName, double balance, int accountNumber){

        this.firstName = firstName;
        this.lastName = lastName;
        this.balance = balance;
        this.accountNumber = accountNumber;
    }

    // ... other methods

    @Override
    public String toString() {
        return "First name: " + firstName + ", Last name: " + lastName +
            ", Account number: " + accountNumber + ", Balance: " + balance;
    }
}

您当前得到的是对象class中定义的toString方法的default implementation,它..

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

在 class 帐户中,您应该覆盖 "toString" 方法。例如。像那样:

@Override
public string toString() {
    return "Account{owner=" + firstName + " " + lastName + "; balance=" + balance + "; accountNumber=" + accountNumber + "}";
}

这是将对象添加到字符串时自动调用的函数。例如

System.out.print(new Account("Josh", "Sad", 10, 10) + " is cool");

你会得到这个:

Account{owner=Josh Sad; balance=10; accountNumber=10} is cool

覆盖 toString()

@Override
public string toString() {
    return "Name: " + lastName + ", " + firstName + "\nbalance: " + balance + "\n accountNumber: " + accountNumber;
}