3
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 Bill’s daughter and implements the class Comparable
. However, when using the method sort()
of Collections
, get the error message:
The method Sort(List) in the type Collections is not applicable for the Arguments (List).
I can’t find the problem in the code. Can anyone help?
Classe Conta:
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;
}
}
You can post (the relevant parts) of the class
ContaCorrente
?– Jéf Bueno
Implement the interface
Comparable
in ClassConta
and define the comparison method. The following link will help you: http://blog.caelum.com.br/sort/– Phrxn
@jbueno done in the answers
– icarobuzz
Icaro, click [Edit] when adding information to your post. Don’t add replies to this.
– Jéf Bueno
All right, thanks for the tip. I’m new to this xD
– icarobuzz