Collections.Sort [Erro]

11 respostas
R
public class Main {

	public static void main(String[] args){
              ArrayList list = new ArrayList();
		AvailList l = new AvailList(3,3);
		AvailList l1 = new AvailList(2,2);
		AvailList l2 = new AvailList(1,1);
		AvailList l3 = new AvailList(4,4);
		AvailList l4 = new AvailList(0,0);
		list.add(l);
		list.add(l1);
		list.add(l2);
		list.add(l3);
		list.add(l4);
		System.out.println(list);
		Collections.sort(list);
		System.out.println(list);
}
public class AvailList implements Comparable{
	private int tamanho;
	private long endereco;
	
	public AvailList(){
		super();
	}
	
	public AvailList(int tamanho, long endereco){
		this.tamanho = tamanho;
		this.endereco = endereco;
	}

	public int getTamanho() {
		return tamanho;
	}

	public void setTamanho(int tamanho) {
		this.tamanho = tamanho;
	}

	public long getEndereco() {
		return endereco;
	}

	public void setEndereco(long endereco) {
		this.endereco = endereco;
	}

	public String toString() {
		return "AvailList [tamanho=" + tamanho + ", endereco=" + endereco + "]";
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (endereco ^ (endereco >>> 32));
		result = prime * result + tamanho;
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AvailList other = (AvailList) obj;
		if (tamanho != other.tamanho)
			return false;
		return true;
	}

	public int compareTo( Object o ) {
		if ( o instanceof AvailList ) {
		AvailList emp = (AvailList) o;
		if ( this.tamanho < emp.tamanho )
			return -1;
		else if ( this.tamanho > emp.tamanho )
			return 1;
		else
			return 0;
		}
		else { 
			return 0; 
		}
	}

}

o erro eh esse aki...

Exception in thread "main" java.lang.ClassCastException: base.AvailList cannot be cast to java.lang.Comparable
	at java.util.Arrays.mergeSort(Unknown Source)
	at java.util.Arrays.sort(Unknown Source)
	at java.util.Collections.sort(Unknown Source)
	at Main.main(Main.java:30)

alguem me ajuda ai...

grato!

11 Respostas

ViniGodoy
Por que seu código está em Java 4?
public class Main {
	public static void main(String[] args){
		List<AvailList> list = new ArrayList<AvailList>();
		list.add(new AvailList(3,3));
		list.add(new AvailList(2,2));
		list.add(new AvailList(1,1));
		list.add(new AvailList(4,4));
		list.add(new AvailList(0,0));
		System.out.println(list);
		Collections.sort(list);
		System.out.println(list);
}
public class AvailList implements Comparable<AvailList>{
	private int tamanho;
	private long endereco;
	
	public AvailList(){
		super();
	}
	
	public AvailList(int tamanho, long endereco){
		this.tamanho = tamanho;
		this.endereco = endereco;
	}

	public int getTamanho() {
		return tamanho;
	}

	public void setTamanho(int tamanho) {
		this.tamanho = tamanho;
	}

	public long getEndereco() {
		return endereco;
	}

	public void setEndereco(long endereco) {
		this.endereco = endereco;
	}

	@Override
	public String toString() {
		return "AvailList [tamanho=" + tamanho + ", endereco=" + endereco + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (endereco ^ (endereco >>> 32));
		result = prime * result + tamanho;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AvailList other = (AvailList) obj;
		if (tamanho != other.tamanho)
			return false;
		return true;
	}

	@Override
	public int compareTo(AvailList emp) {
		if ( this.tamanho < emp.tamanho )
	             return -1;
		if ( this.tamanho > emp.tamanho )
		     return 1;
		return 0;
	}
}
P

Aqui você vai encontrar como implementar o comparable ou comparator


ViniGodoy

Tem uma ótima explicação sobre isso no GUJ também:

R

como eu mudo do java 4?
nao entendi…
grato!

ViniGodoy

Já postei uma versão do seu código em Java 6.

Sua sintaxe estava antiquada, mas essa provavelmente não é a causa do problema. Você tem certeza que aquele Comparable se refere a classe java.lang.Comparable?

R

continua com o mesmo erro…problema e que eh essa mesmo…

ViniGodoy
Rodei aqui e funcionou. Veja como ficou meu arquivo Main.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
	public static void main(String[] args){
		List<AvailList> list = new ArrayList<AvailList>();
		list.add(new AvailList(3,3));
		list.add(new AvailList(2,2));
		list.add(new AvailList(1,1));
		list.add(new AvailList(4,4));
		list.add(new AvailList(0,0));
		System.out.println(list);
		Collections.sort(list);
		System.out.println(list);
	}
}
E o meu arquivo AvailList.java
public class AvailList implements Comparable<AvailList>{
	private int tamanho;
	private long endereco;
	
	public AvailList(){
		super();
	}
	
	public AvailList(int tamanho, long endereco){
		this.tamanho = tamanho;
		this.endereco = endereco;
	}

	public int getTamanho() {
		return tamanho;
	}

	public void setTamanho(int tamanho) {
		this.tamanho = tamanho;
	}

	public long getEndereco() {
		return endereco;
	}

	public void setEndereco(long endereco) {
		this.endereco = endereco;
	}

	@Override
	public String toString() {
		return "AvailList [tamanho=" + tamanho + ", endereco=" + endereco + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (endereco ^ (endereco >>> 32));
		result = prime * result + tamanho;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AvailList other = (AvailList) obj;
		if (tamanho != other.tamanho)
			return false;
		return true;
	}

	@Override
	public int compareTo(AvailList emp) {
		if ( this.tamanho < emp.tamanho )
	             return -1;
		if ( this.tamanho > emp.tamanho )
		     return 1;
		return 0;
	}
}
R

copiei e colei mas nao funciona… o eclipse 3.6.2 nao compila com erro…
aff…
pode ser o eclipse?

grato

ViniGodoy

Eu criei no eclipse também. Tente recriar o projeto.

R

funcinou…
mas pq acontece isso?
tem alguma maneira de evitar isso?
grato

ViniGodoy

Isso geralmente não acontece.

Não sei o que houve em seu projeto antigo, pois não sei como foi configurado. Mas as possíveis causas são:

a) Você criou um projeto que usa uma versão antiga, muito antiga, de Java;

b) Você tem alguma outra classe chamada Comparable no seu projeto e está usando essa classe sem ver;

Agora, um projeto no Eclipse não “estraga” sozinho.

Criado 10 de julho de 2011
Ultima resposta 10 de jul. de 2011
Respostas 11
Participantes 3