gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:08:03 29 4
gpt4 key购买 nike

我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,并在其他 vector (javaVector)上使用Java排序并比较它们。

我做了测试,但没有成功,所以我不知道是什么失败了。*注意:西类牙语中有几个单词:ordenado = 排序、Ejemplo = 示例、maximo = maximun、contenido = 内容。

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只是比较,它给出了正确的

我尝试创建一个未排序的 vector 添加元素,但出现了 java.lang.ClassCastException: [Ljava.... 等

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 接口(interface)。或者您需要在比较过程中提供Comparator

关于java.lang.ClassCastException : [Ljava. lang.Object;无法转换为 [Ljava.lang.Comparable;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15862409/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com