构造函数未定义

时间:2013-03-12 12:19:49

标签: java arrays constructor undefined

在我的主要课程中,我有以下代码:

Polynomial P = new Polynomial(polynomial);

在另一个课程中,我有以下代码:

public class Polynomial {

    private int[] polynomial;

    public Polynomial(int[] polynomial) {
        this.polynomial = polynomial;
    }
}

为什么构造函数Polynomial(int [])未定义?

顺便说一句......主类中的多项式指向:

int [] polynomial = new int[count];

这是完整的主要课程:

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Arrays;
public class Main {

public static void main(String[] args) {
    String input = JOptionPane.showInputDialog(null, "This is the Polynomial Cal" +
            "culator Menu. Please ente" +
                "r your menu selection:\n (1) Enter c" +
                    "oefficients of polynomial P(x). \n (2) Enter co" +
                    "efficients of polynomial Q(x). \n (3) Sum polynomi" +
                        "als P(x) and Q(x). \n (4) Multiply polynomials P(x) and Q(" +
                            "x). \n (5) Quit.", "Polynomial Menu",JOptionPane.PLAIN_MESSAGE);
    Scanner inputScanner =new Scanner(input);           //Scanner for Menu
    int userChoice = inputScanner.nextInt();            //Menu Choice
    if(userChoice>=1 && userChoice<=5)                  //User Input Catch
    {
        switch(userChoice)
        {
        case 1: String coefficientInput= JOptionPane.showInputDialog(null, "Please enter th" +
                "e coefficients of the terms in the polynom" +
                    "ial.(Ax^n, Bx^(n-1)...Yx,Z) \n Only ent" +
                        "er the values of the coeffien" +
                            "ts i.e (A + B - C + D) ");
                Scanner countScanner = new Scanner(coefficientInput);       //Scanner for count
                int coefficient= countScanner.nextInt();
                int count=1;
                while(countScanner.hasNextInt())
                {
                    count++;
                    countScanner.nextInt();
                }
                int [] polynomial = new int[count];                         //Size of Array=Count
                Scanner coefficientScanner = new Scanner(coefficientInput);
                int term = 0;
                System.out.println(count);
                int i=0;
                while(coefficientScanner.hasNextInt())                                  //Initialisation of array
                {
                    term=coefficientScanner.nextInt();
                    polynomial[i]=term;
                    i++;
                }
                Polynomial P = new Polynomial(polynomial);

        }
    }
    else
    {
        JOptionPane.showMessageDialog(null, "No option selected. Please try again.","Input Error",JOptionPane.ERROR_MESSAGE);
    }

}

}

多项式P =新多项式(多项式)

时发生错误

3 个答案:

答案 0 :(得分:2)

我猜测主类中名为polynomial的引用不指向int []。如果它是Polynomial类型的引用,您将需要创建另一个构造函数,该构造函数接受Polynomial(也称为“复制构造函数”)或更改polynomial的类型。

我不喜欢你编写那个构造函数的方式。这不是私密的;你传入的引用是可变的。做一个防御性的副本。我就是这样做的:

public class Polynomial {

    private int[] coefficients;

    public Polynomial(int[] coefficients) {
        if (coefficients == null) throw new IllegalArgumentException("coefficients cannot be null");
        this.coefficients = new int[coefficients.length];
        System.arraycopy(0, coefficients, 0, this.coefficients, this.coefficients.length);
    }

    public Polynomial(Polynomial p) {
        this(p.coefficients);
    }
}

这也是一个天真的设计。没有浮点系数?如果你想对像y = x^1000 + 1这样的东西建模,效率很低。你将在一个非常大的数组中有两个非零系数。

更好的设计是创建Monomial并让Polynomial维护List

答案 1 :(得分:0)

以下代码将很快编译,您的类定义和构造函数没有问题。

注意如何定义和初始化整数数组。

public static void main(String[] args){
    int[] polynomial = new int[]{2, 1, 2};
    Polynomial P = new Polynomial(polynomial);
}

答案 2 :(得分:0)

好的。我刚刚删除了一行代码并重新输入了 - 然后它才有效。刚刚发生了什么?