Studying collections, I tried to use the following example in Eclipse:
public class TestaLista {
public static void main(String[] args) {
List <Conta> list = new ArrayList<Conta>();
Conta cc1 = new ContaCorrente();
cc1.setNumero(10);
Conta cc2 = new ContaCorrente();
cc2.setNumero(5);
Conta cc3 = new ContaCorrente();
cc3.setNumero(2);
list.add(cc1);
list.add(cc2);
list.add(cc3);
System.out.println(list);
Collections.sort(list);
System.out.println("------------");
System.out.println(list);
}
}
My class ContaCorrente
is the daughter of Account and implements the Comparable
class. However, when I use the method sort()
of Collections
, I get the error message:
The method sort (List) in the type Collections is not applicable for the arguments (List).
I can not find the problem in the code. Does anyone help?
Account Class:
public abstract class Conta {
protected double saldo;
protected int numero;
private int agencia;
private String nome;
public double getSaldo() {
return saldo;
}
public void deposita(double valor) {
if (valor > 0) {
this.saldo += valor;
} else {
throw new ValorInvalidoException(valor);
}
System.out.println("Fim do deposita");
}
public void saca(double valor) {
this.saldo -= valor;
}
public abstract void atualiza(double taxaSelic);
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
public int getAgencia() {
return agencia;
}
public void setAgencia(int agencia) {
this.agencia = agencia;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Conta)){
return false;
}
Conta c = (Conta)obj;
return this.numero == c.numero || this.nome.equals(c.nome);
}
@Override
public String toString() {
return "Conta de número " + this.numero;
}
}