簡體   English   中英

java.lang.ClassCastException:[Ljava.lang.Object; 無法轉換為[Ljava.lang.Comparable;

[英]java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;

我正在使用Eclipse而我正在使用Java。 我的目標是要排序的向量,在適應我型矢量的一個矢量(vectorExample)BOGO排序方法和使用其他載體(javaVector)Java排序和比較。

我做了測試,但它沒有用,所以我不知道什么是失敗的。 *注意:西班牙語中的單詞很少: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類

包元素;

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;
    }
}

}

測試首先,它是一個愚蠢的測試,因為它使要素,以使...真的地圖無法做任何事情的方法,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());
    }
}

對不起,對於問題和感謝。

問題是您的測試類ElementoTest應該實現Comparable接口。 或者您需要在Comparator期間提供比較器。

您的ElementtoTestElementtoTest實現了Comparable

如果沒有,它需要。

我懷疑它沒有,因為這正是導致這個錯誤的原因。 您需要添加implements Comparable ,然后覆蓋int compareTo(Elementtotest e)方法,您可以在其中指定您希望基於的對象排序的條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM