2013-04-07 85 views
0

我正在使用Eclipse,我正在使用Java。我的目標是到排序載體,與BOGO排序方法 以適合我的類型向量的一個向量(vectorExample)以及其他載體(javaVector)使用的Java排序並進行比較。java.lang.ClassCastException:[Ljava.lang.Object;不能轉換爲[Ljava.lang.Comparable;

我做了測試,但它沒有工作,所以我不知道什麼是失敗。 *注意:西班牙文中幾乎沒有單詞: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; 
    } 
} 

}

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

對不起,對於這些問題和謝謝。

+0

發佈完整堆棧跟蹤 – BackSlash 2013-04-07 12:33:07

回答

1

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

+0

:/有此在ElementoTest 公共類ElementoTest **實現可比 **,序列化{ \t私有靜態最後長的serialVersionUID = -7683744298261205956L; 我不知道你是否意味着 – 2013-04-07 12:48:39

1

您的班級ElementtoTest實施Comparable

如果沒有,它需要。

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

+0

:/在ElementoTest有這個 公共類ElementoTest **實現可比 **,序列化{ \t私有靜態最後的serialVersionUID長= -7683744298261205956L; 我不確定你的意思 – 2013-04-07 12:47:23

+0

你有沒有添加一個方法'公衆int compareTo(ElementoTest)'? – drewmoore 2013-04-07 12:50:57

+0

而且也: @覆蓋 \t公衆詮釋的compareTo(ElementoTest ELEMENTO){ \t \t如果(ELEMENTO == NULL){ \t \t \t返回-1; \t \t} else if(this.equals(elemento)){ \t \t \t return 0; \t \t} else if(clave 2013-04-07 12:50:57

相關問題