无法将自定义 object 数据类型的 ArrayList 转换为相应的常规数组

Cannot convert an ArrayList with my custom object as its data type into the corresponding regular array

正如标题所说,我无法将我的 ArrayList 转换为 Array。我的 ArrayList 的数据类型是 自定义 object 但我似乎无法找到我的问题所在。在程序 运行 之前,它给出的错误不会显示为问题。前两个 classes 是 objects,然后我有一个 class calle Tester,其中主要方法是。

Class出现错误的地方:

package backend;

import java.util.ArrayList;

public class Function {

Coefficient[] coefArray;
int constant;

public Function(ArrayList<Coefficient> coefFunction, int constant){
    Coefficient[] coefArray = (Coefficient[]) coefFunction.toArray();
    this.coefArray = sortArray(coefArray);
    this.constant = constant;
}

private Coefficient[] sortArray(Coefficient[] newArray){
    int tempPow = -1000000000;
    Coefficient[] sortedArray = new Coefficient[newArray.length];
    sortedArray = null;
    for(int i=0; isFull(sortedArray); i++){
        for(Coefficient coef : newArray){
            if(coef.pow>tempPow){
                tempPow = coef.pow;
                sortedArray[i] = coef;
            }
        }
    }
    return sortedArray;
}

private boolean isFull(Coefficient[] anArray){
    for(Coefficient i : anArray) {
        if(i == null) return true;
      }
     return false;
}

public String toString(){
    String compiledString="";
    for(Coefficient coef : coefArray){
        compiledString += coef.toString()+"+";
    }
    if(constant==0){
        //No constant there
    }else{
        compiledString = compiledString + constant;
    }
    return compiledString;
}
}

系数Class:

package backend;

public class Coefficient {

String stringVersion;
public int pow;
public int coefInteger;
public double coefDouble;

//Constructor for variable with a coefficient that is non-fractal and a power higher than 1
public Coefficient(int coef, int pow){
    this.coefInteger = coef;
    this.pow = pow;
    switch(coef){
        case 0:
            //Do nothing here
        case 1:
            this.stringVersion = "x^"+pow;
            break;
        default:
            this.stringVersion = coef+"x^"+pow;
            break;
    }
}

//Constructor for variable with a coefficient that is fractal and a power higher than 1
public Coefficient(double coef, int pow){
    this.coefDouble = coef;
    this.pow = pow;
    this.stringVersion = coef+"x^"+pow;
}

//Constructor for variable with a coefficient but no power
public Coefficient(double coef){
    this.coefDouble = coef;
    this.pow = 1;
    this.stringVersion = coef+"x";
}

//Constructor for variable with a coefficient but no power
public Coefficient(int coef){
    this.coefInteger = coef;
    this.pow = 1;
    this.stringVersion = coef+"x";
}

public String toString(){
    return stringVersion;
}
}

测试人员:

package backend;

import java.util.ArrayList;
import java.util.Scanner;

public class Tester {
public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    ArrayList<Coefficient> function = new ArrayList<Coefficient>();
    Coefficient xVal;
    Function printFunction;
    System.out.println("Enter the degree of the equation:");
    int pow = scan.nextInt();
    System.out.println("Enter the x components of the function in the order of coefficient then power. Enter constant last:");
        double coef = 0;
        while(pow>0){
            coef = scan.nextDouble();
            if(pow==1){
                if((int) coef == coef){
                    xVal = new Coefficient((int) coef);
                }else{
                    xVal = new Coefficient(coef);
                }
            }else{
                if((int) coef == coef){
                    xVal = new Coefficient((int) coef, pow);
                }else{
                    xVal = new Coefficient(coef, pow);
                }
            }
            function.add(xVal);
            pow--;
        }

        System.out.println("Enter the constant:");
        printFunction = new Function(function, scan.nextInt());
        System.out.println(printFunction.toString());


}
}

错误:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
cannot be cast to [Lbackend.Coefficient;
at backend.Function.<init>(Function.java:11)
at backend.Tester.main(Tester.java:36)

非常感谢任何帮助。如果您看到其他需要修复的地方,请指出。

错误在这一行:

Coefficient[] coefArray = (Coefficient[]) coefFunction.toArray();

如果你阅读 toArray() 的 javadoc,你会发现它 returns 一个 Object[],你不能简单地转换为 Coefficient[].

改为使用toArray(T[] a):

Coefficient[] coefArray = coefFunction.toArray(new Coefficient[coefFunction.size()]);

免责声明:我没有审查您的其余代码,因此没有任何评论并不意味着其他一切都很好。