java.lang.ClassCastException:[Ljava.lang.Object;无法转换为[Ljava.lang.Comparable;

时间:2013-04-07 12:32:24

标签: java sorting vector comparable lang

我正在使用Eclipse而我正在使用Java。我的目标是使用 bogoSort 方法排序一个向量 在一个矢量( vectorExample )中适应我的矢量类型,并在其他矢量( javaVector )上使用 Java sort 并进行比较。

我做了测试,但它没有用,所以我不知道什么是失败的。 *注意:西班牙语中的单词很少:ordenado = sorted,Ejemplo = Example,maximo = maximun,contenido = content。

EjemploVector类

   package vector;
import java.util.NoSuchElementException;
import java.util.Vector;
import java.util.Iterator;

public class EjemploVector <T> {
protected T[] contenido;
private int numeroElementos;

@SuppressWarnings("unchecked")
public EjemploVector () {
    contenido = (T[]) new Object[100];
    numeroElementos = 0;
}

@SuppressWarnings("unchecked")
public EjemploVector (int maximo) {
    contenido = (T[]) new Object[maximo];
    numeroElementos = 0;
}

public String toString(){
    String toString="[";
     for (int k=0; k<numeroElementos;k++){
         if (k==numeroElementos-1){
             toString = toString + contenido[k].toString();
         } else {
             toString = toString + contenido[k].toString()+", ";
         }
     }
     toString = toString + "]";
     return toString;
}

public boolean equals (Object derecho){
     if (!(derecho instanceof Vector<?>)) {
         return false;
     } else if (numeroElementos != ((Vector<?>)derecho).size()) {
         return false;
     } else {
         Iterator<?> elemento = ((Vector<?>)derecho).iterator();
         for (int k=0; k<numeroElementos;k++){
             if (!((contenido[k]).equals (elemento.next()))) {
                 return false;
             }
         }
         return true;
     }
}

public void addElement (T elemento){
    contenido[numeroElementos++]= elemento;
}

protected T[] getContenido(){
    return this.contenido;
}

protected T getContenido (int k){
    return this.contenido[k];
}

@SuppressWarnings("unchecked")
protected void setContenido (int k, Object elemento){
    this.contenido[k]= (T)elemento;
}
  

EjemploVectorOrdenadoClass

package vector.ordenado;

import java.util.Arrays;
import java.util.Random;

import vector.EjemploVector;

public class EjemploVectorOrdenado<T extends Comparable<T>> extends EjemploVector<T> {

    private boolean organized;

    public EjemploVectorOrdenado() {
        super();
        organized = true;
    }

    public EjemploVectorOrdenado(int maximo) {
        super(maximo);
        organized = true; //
    }

    public boolean getOrdenado() {
        return this.organized;
    }

    // Method bogoSort
    public void bogoSort() {
        if (!this.organized) {

            if (this.size() > 0) {


                Random generator;
                T tempVariable;
                int randomPosition;

                do {
                    generator = new Random();

                    for (int i = 0; i < this.size(); i++) {
                        randomPosition = generator.nextInt(this.size());

                        tempVariable = contenido[i];
                        contenido[i] = contenido[randomPosition];
                        contenido[randomPosition] = tempVariable;

                    }
                } while (!organized);

            }

        }
        this.organized = true;
    }

    public void addElement(T elemento) {
        super.addElement(elemento);
        if (organized && this.size() > 1) {
            T penultimo = this.getContenido(this.size() - 2);
            T ultimo = this.getContenido(this.size() - 1);
            organized = penultimo.compareTo(ultimo) <= 0;
        }
    }
}

ElementoTest类

  

package elementos;

import java.io.Serializable;

public class ElementoTest implements Comparable<ElementoTest>, Serializable {
private static final long serialVersionUID = -7683744298261205956L;

private static int numeroElementosTest = 0;
private int clave;
private int  valor;

public ElementoTest(int i){
    this.clave = i;
    this.valor = numeroElementosTest;
    numeroElementosTest++;
}

public String toString(){
    return ("(" + this.clave + "," + this.valor + ")");
}

public boolean equals (Object derecho){
     if (!(derecho instanceof ElementoTest)) {
         return false;
     } else {
         return clave == ((ElementoTest)derecho).clave;
     }
}

public char getClave(){
      return this.clave;
}

public int getValor(){
      return this.valor;
}

@Override
public int compareTo(ElementoTest elemento) {
    if (elemento == null){
        return -1;
    } else if (this.equals(elemento)){
        return 0;
    } else if (clave < elemento.clave){
        return -1;
    } else {
        return 1;
    }
}

}

TESTS 第一个是一个愚蠢的测试,因为它按顺序排列元素所以...真的方法没有做任何事情,java只是比较它给出了正确的

我尝试制作未分类的向量添加元素,但出现了java.lang.ClassCastException: [Ljava.... etc.

package vector.ordenado;

import static org.junit.Assert.*;

import java.util.Collections;
import java.util.Vector;

import org.junit.Before;
import org.junit.Test;

import elementos.ElementoTest;

public class EjemploVectorOrdenadoTest {

    private Vector<ElementoTest> vectorJava;
    private EjemploVectorOrdenado<ElementoTest> vectorExample;

    @Before
    public void setUp() throws Exception {
        vectorJava = new Vector<ElementoTest>(100);
        vectorExample = new EjemploVectorOrdenado<ElementoTest>(100);
    }

    @Test
    public void testSortFailTest() {
        for (char c = 'a'; c < 'g'; c++) {
            vectorJava.addElement(new ElementoTest(c));
            vectorExample.addElement(new ElementoTest(c));

        }
        Collections.sort(vectorJava);
        vectorExample.bogoSort();
        assertTrue(vectorExample.equals(vectorJava));
        assertTrue(vectorExample.getOrdenado());
    }

    @Test
    public void testSort() {
        {
            vectorJava.addElement(new ElementoTest(1));
            vectorJava.addElement(new ElementoTest(3));
            vectorJava.addElement(new ElementoTest(2));
            vectorExample.addElement(new ElementoTest(3));
            vectorExample.addElement(new ElementoTest(2));
            vectorExample.addElement(new ElementoTest(1));

        }
        Collections.sort(vectorJava);
        vectorExample.bogoSort();
        assertTrue(vectorExample.equals(vectorJava));
        assertTrue(vectorExample.getOrdenado());
    }
}

抱歉,问题和感谢。

2 个答案:

答案 0 :(得分:1)

问题是您的测试类ElementoTest应该实现Comparable接口。或者您需要在比较期间提供Comparator

答案 1 :(得分:1)

您的班级ElementtoTest是否实施Comparable

如果没有,则需要。

我怀疑它没有,因为这正是造成这个错误的原因。您需要添加implements Comparable,然后覆盖int compareTo(Elementtotest e)方法,在此方法中,您可以根据需要指定要对对象进行排序的条件。