對於其中一個類中的賦值,我必須添加由int數組組成的兩個Poly對象。我有一個叫做add的方法,它應該將Poly作爲參數並將其添加到當前Poly對象(Poly1.add(Poly2))中,Poly表示一個多項式,所以它應該添加每個數組的係數。我如何從Poly的每個陣列中取出所有可以添加它們的陣列?如何添加到以int數組爲參數的對象
public class Poly
{
private int[] coefficients2;
public static void main(String[] args){
}
Poly(int[] coefficients){
coefficient2 = coefficients;
}
public Poly add(Poly a)
{
Poly b = new Poly(coefficientList);
Poly c = null;
if (coefficientList.length-1 >= a.degree())
{
c = new Poly(new int[coefficientList.length-1]);
}
else if (coefficientList.length-1 < a.degree())
{
c = new Poly(new int[a.degree()]);
}
//This is where I don't know what to do.
}
}
您可能需要某種循環,在其中您可能會執行像'c.coefficients2 [i] = coefficients2 [i] + a.coefficients2 [i];(不會發布完整答案,因爲此操作是家庭作業) –