java - java优先级队列

标签 java priority-queue

我正在编写一个priorityqueue类,我想根据帐户余额对其进行排序和打印。它打印值,但问题是它打印传递给构造函数的参数的十六进制值。我的代码哪里出错了?

帐户:

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;*/


    }

}

AccountApp:

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

您当前获得的是 default implementation Object 类中定义的 toString 方法,其中..

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())

关于java - java优先级队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31702079/

相关文章:

java - 如何使用 Android 在 Cloud Firestore 中的特定文档之后查询集合?

java - 如何删除 java.util.Set 中的最后一个元素?

java - PriorityQueue 元素未排序

c++ - 你如何在 C++ 中的 priority_queue 中排序对象?

apache-kafka - 有没有办法在 Apache Kafka 2.0 中确定消息的优先级?

java - 使用maven创建可执行jar,其中可以替换库

java - 在 Spring Boot 应用程序中修改 Activity 配置文件并刷新 ApplicationContext 运行时

java - 在 Java 中是否有更优雅的处理列表的方法? (Python 与 Java)

c++ - 使用 deque 实例化 priority_queue 无法编译 - 使用 vector 则可以。这是为什么?

JAVA-PriorityQueue实现