BankAccounts의 배열 목록에 비교 가능한 인터페이스를 구현하려고했지만, 주요 오류가 발생했습니다. 특히, Collections.sort (목록)에 대한 테스터 클래스의 main 메소드를 컴파일하고 실행할 때) 선. 이 온라인과 자바 독을 통해 syntax..looked와 내가 잘못 어디 찾을 수없는 인식하지 않는 것을 말하는 ..Comparable interface sorting method not working
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 (목록)를 사용할 수 없습니다. (유추 된 형식이 선언 바인딩 (들) 추론 : BankAccount가 (들) 바인딩 : Comparable를) T # 1, T # 2는 타입 변수 : T # 1 오브젝트 방법 정렬 (목록, 비교기) T # 2에 선언 연장 메서드 sort (List)에 선언 된 Comparable 1 개의 오류
이 도움이됩니다. ** 경고 ** FP 반올림 오류로 인해 부동 소수점 값에 ==를 사용하지 마십시오. – Darien
대신에 'balance - temp.balance'와 같은 것을 사용할 수 있습니다 - 예제가 컴파일되지 않는다는 사실을 분별하지 마십시오) – MadProgrammer
오류 메시지가 @Darien보다 위에 있습니다 – user2809437