3

ドライバー クラスから整数オブジェクトを、作成した SortedArray ジェネリック クラスの関数の引数として渡そうとすると問題が発生します。ドライバー クラスから、ユーザーの int 入力を Integer オブジェクトに変換して、SortedArray クラスの Comparable にキャストします。

「スレッド "main" java.lang.ClassCastException の例外: java.lang.Integer を Comparable にキャストできません」というエラーが引き続き表示されます。私はクラスメートのソースコードのいくつかを見て、引数/パラメータの設定にほとんど違いがないことを発見しましたが、彼らのコードはうまく機能しています. 私は自分が犯したエラーを見つけようと何時間も探していましたが、Integer オブジェクトを Comparable にキャストできない理由をまだ見つけられません。

ここに私のSortedArrayクラスからのビットがあります

public class SortedArray implements Comparable{
    public int size;
public int increment;
public int top;
    Comparable[] a = new Comparable [size];

public SortedArray(int initialSize, int incrementAmount)
{
        top = -1;
        size = initialSize;
        increment = incrementAmount;

}
public int appropriatePosition(Comparable value)
{
        int hold = 0;
        if(top == -1)
        {
            return 0;
        }
        else
        {    
            for(int i = 0; i <= top; i++)
            {
                if(a[i].compareTo(value) > 0)
                {
                    hold = i;
                    break;
                }
            }
        }
        return hold;
}

public void insert(Comparable value) //The function that my driver class needs to access
{
        //Shifting numbers to the top
        if(full() == true)
        {
            Comparable[] tempArray = new Comparable[top + increment];
            for(int i= 0; i< size; i++)
            {
                tempArray[i]= a[i];
                a  = tempArray;
            }
            size = top + increment;
        }
        if(a[appropriatePosition(value) + 1] != null)
        {
            for(int i = top; i < appropriatePosition(value); i--)
            {
                a[i + 1] = a[i];
            }
        }
        a[appropriatePosition(value) + 1]= value;
    }

これは、Integer Object insertObj を SortedArray の挿入関数の引数として渡すドライバ クラスのコードです。

public class IntDriver  {
 public static void main(String[] args)
 {
     Scanner keyboard = new Scanner(System.in);
     //Creating variables
     int data;
     boolean check = false;
     int choice;
     int size = 5;
     int increment = 3;
     SortedArray b = new SortedArray(size, increment);
     //Creating Menu
     while(check == false)
     {
     System.out.println("Please choose through options 1-6.");
     System.out.println("1. Insert\n2. Delete\n3. Clear\n4. Smallest\n5. Largest\n6. Exit");
     choice = keyboard.nextInt();
     switch(choice)
         {
         case 1:
             System.out.println("Type the int data to store in array location.");
             data = keyboard.nextInt();
             Integer insertObj = new Integer(data);
             b.insert(insertObj);// Here's where I lay "insertObj" as an argument for the SortedArray function.
             System.out.println("The value " + data + " is inserted");
            break;
4

3 に答える 3

6

問題は、Integer拡張することです。java.lang.Comparableその後、あなたComparableは ではありませんjava.lang.Comparable。エラー メッセージを見てComparableくださいjava.lang

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable

つまりjava.lang.Integer、独自のクラスにキャストすることはできません

于 2010-01-21T20:22:28.343 に答える
5

axtavtで述べたように、問題は独自のクラス Comparable があることです。より具体的には、次のことを意味します。

Integer.valueOf(1) instanceof java.util.Comparable == true
Integer.valueOf(1) instanceof Comparable == false

これは、コードのどこかに次のようなものがあることを意味します。

Object[] a = new Object[] {Integer.valueOf(1);};
Comparable x = (Comparable) a[0];
// or something equivalent, this is likely being passed through layers
// and not being done next to each other like this.

それを次のように変更する必要があります。

Object[] a = new Object[] {Integer.valueOf(1);};
java.util.Comparable x = (java.util.Comparable) a[0];

さらに良いことに、Comparator クラスの名前を、Java の標準クラスと衝突しない名前に変更する必要があります。一般に、Java には名前空間がありますが、この種の混乱を避けるために、クラスがシステム クラスと同じ名前になるのを避けるようにしてください。

于 2010-01-21T20:32:25.877 に答える
0

私はこれにあまり力を入れていませんが、独自のクラスを作成するよりも、SortedSet実装 (またはその子インターフェイスNavigableSet)を使用する方が簡単ではないでしょうか? TreeSetつまり、重複した要素が必要でない限り...

于 2010-01-21T20:11:01.880 に答える