2013-10-29 67 views
-1

我試圖在BankAccounts的數組列表上實現可比較的接口,但是當我嘗試編譯和運行測試器類的主要方法時,我遇到了重大錯誤,具體是關於Collection.sort(list )線。它說,它不承認通過網上和javadoc的syntax..looked,我找不到在哪裏我錯了..可比較的接口排序方法不工作

public class BankAccount implements Comparable { //QUESTION 2.1 
    /** 
    A bank account has a balance that can be changed by 
    deposits and withdrawals. 
*/ 
    private int accountNumber; 
    private double balance; 
    /** 
    Constructs a bank account with a zero balance 
    @param anAccountNumber the account number for this account 
    */ 
    public BankAccount(int anAccountNumber) 
    { 
    accountNumber = anAccountNumber; 
    balance = 0; 
    } 

    /** 
    Constructs a bank account with a given balance 
    @param anAccountNumber the account number for this account 
    @param initialBalance the initial balance 

*/ 公衆的BankAccount(INT anAccountNumber,雙initialBalance) {
accountNumber = anAccountNumber; balance = initialBalance; }

/** 
    Gets the account number of this bank account. 
    @return the account number 
    */ 
    public int getAccountNumber() 
    { 
    return accountNumber; 
    } 

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


    public int compareTo (BankAccount temp) { 


    if (balance<temp.balance) 
     return -1; 
    if (balance==temp.balance) 
     return 0; 
    return 1; 
    } 

} 

public class TestSortedBankAccounts { 

    public static void main(String[] args) { 
     // Put bank accounts into a list 
     ArrayList<BankAccount> list = new ArrayList<BankAccount>(); 

     BankAccount ba1 = new BankAccount(100, 500); //Constructor acctNumber and balance 
     BankAccount ba2 = new BankAccount(200, 10000); 
     BankAccount ba3 = new BankAccount(300, 400); 
     BankAccount ba4 = new BankAccount(600, 0); 
     BankAccount ba5 = new BankAccount(800, 50); 

     list.add(ba1); 
     list.add(ba2); 
     list.add(ba3); 
     list.add(ba4); 
     list.add(ba5); 

     // Call the library sort method 
     Collections.sort(list); 

     // Print out the sorted list 
     for (int i = 0; i < list.size(); i++) { 
      BankAccount b = list.get(i); 
      System.out.println(b.getBalance()); 
     } 
    } 
} 

UPDATE:TestSortedBankAccounts.java:26:錯誤:找到的排序(ArrayList的) Collections.sort(列表)沒有合適的方法; ^ 方法Collections.sort(List,Comparator)不適用 (不能從參數中實例化,因爲實際和正式參數列表的長度不同) 方法Collections.sort(List)不適用 (推斷的類型不符合聲明綁定 推斷:BankAccount bound(s):可比較的) 其中T#1,T#2是類型變量: T#1擴展在方法排序中聲明的對象(List,Comparator) T#2擴展在方法排序(列表)中聲明的Comparable 1錯誤

+0

它幫助,如果您發佈的錯誤信息。 **警告**由於FP舍入錯誤,請勿對浮點值執行==。 – Darien

+2

你可以使用'balance - temp.balance'而不是 - 我們也不會猜測你的例子不會編譯的事實;) – MadProgrammer

+0

錯誤信息在@Darien – user2809437

回答

4

您正在實施原始版本Comparable。您應該實現的Comparable的一般形式:

public class BankAccount implements Comparable<BankAccount> { 

如果要實現原始形式,然後在compareTo參數類型是Object。使用通用形式,您可以提供泛型類型參數作爲您已有的compareTo的參數。

+0

仍然收到錯誤信息,請參閱我上面的編輯 – user2809437

+0

我自己執行了你的代碼(我自己提供必要的2 -Arg'BankAccount'構造函數)並且它可以工作。根據您剛剛提供的錯誤消息,必須有其他代碼不提供。 – rgettman

+0

rgettman有整個代碼。 – user2809437

0

我的回答(即我試過和它的作品):

public class BankAccount implements Comparable<Object> { // <-- 
    /** 
    A bank account has a balance that can be changed by 
    deposits and withdrawals. 
*/ 
    private int accountNumber; 
    private double balance; 
    /** 
    Constructs a bank account with a zero balance 
    @param anAccountNumber the account number for this account 
    */ 
    public BankAccount(int anAccountNumber) 
    { 
    accountNumber = anAccountNumber; 
    balance = 0; 
    } 

    /** 
    Constructs a bank account with a given balance 
    @param anAccountNumber the account number for this account 
    @param initialBalance the initial balance 
    */ 
    public BankAccount(int anAccountNumber, double initialBalance) { 
     accountNumber = anAccountNumber; balance = initialBalance; } 

    /** 
    Gets the account number of this bank account. 
    @return the account number 
    */ 
    public int getAccountNumber() 
    { 
    return accountNumber; 
    } 

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


    // public int compareTo (BankAccount temp) { // <-- WRONG 
    public int compareTo(Object temp){ // <-- RIGHT 

    BankAccount other = (BankAccount)temp; // <-- Cast to BankAccount 
    if (balance<other.balance) 
     return -1; 
    if (balance==other.balance) 
     return 0; 
    return 1; 
    } 

}