我的项目应该对多项式进行基本运算。每个多项式都是来自类Monom的对象的ArrayList。
我使用Swing创建了一个界面。我做了一个将接收到的String转换成Polinom对象的方法。如果我使用基本命令(例如按下按钮,获取文本并显示文本),则一切正常。但是,当我调用前面提到的方法(toPolinom)时,按钮不再起作用,并且控制台中写有红线。

这是按钮代码的样子:

 mybutton = new JButton("Suma");
    mybutton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            String pol1 = numberField1.getText();
            String pol2 = numberField2.getText();
            polinom1 = new Polinom(p1);
            polinom2 = new Polinom(p2);
            polinom1 = polinom1.toPolinom(pol1);
            polinom2 = polinom2.toPolinom(pol2);
            resultField.setText(polinom1.suma(polinom1, polinom2)
                    .toString());

        }
    });

   mybutton2 = new JButton("Diferenta");
    mybutton2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String pol1 = numberField1.getText();
            String pol2 = numberField2.getText();
            polinom1 = polinom1.toPolinom(pol1);
            polinom2 = polinom2.toPolinom(pol2);
            resultField.setText(rezultat.diferenta(polinom1, polinom2)
                    .toString());

        }
    });


resultFieldnumberField1JTextField类型;

这是我来自类toPolinom()Polinom.java方法:

   public  Polinom toPolinom(String p1) {

    List<Monom> prez = new ArrayList<Monom>();
    int grad, coef;
    int i = 0;
    for(i=0; i<p1.length()-1; i=i+5){
        grad=0; coef=0;
        if(p1.charAt(i)=='+') coef=Character.getNumericValue(p1.charAt(i+1));
        else if (p1.charAt(i)=='-') coef=-Character.getNumericValue(p1.charAt(i+1));
        grad=Character.getNumericValue(p1.charAt(i+4));
        prez.add(new MonomZ(grad, coef));
    }


    return new Polinom(prez);
}


我测试了此方法,并且它可以工作。

最后,这是我单击按钮时收到的:

   Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at tema1.Polinom.toString(Polinom.java:208)
at tema1.Fatza$1.actionPerformed(Fatza.java:61)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


警告方法已添加:

  public String toString() {

    String Rezultat = "";
    for (int i = 0; i < polinom.size() - 1; i++) {
        Rezultat = Rezultat + verificare(polinom.get(i), polinom, i);
    }
    return Rezultat
            + verificare(polinom.get(polinom.size() - 1), polinom, //line 208
                    polinom.size() - 1);

}

private String verificare(Monom monom, List<Monom> polinom, int n) {
    // functia verifica daca trebuie adaugat semn sau nu
    String text;
    boolean ok = true;
    for (int i = 0; i < n; i++) {
        if (polinom.get(i).getCoef().doubleValue() != 0) {
            ok = false;
            break;
        }
    }
    double numar = monom.getCoef().doubleValue();
    if (ok == true && numar > 0) {
        text = monom.toString();
    } else {
        text = "+" + monom.toString();
    }
    if (numar < 0) {// numarul are inclus semn negativ
        text = monom.toString();
    }
    return text;
}


如果我忘记了任何信息,请询问我。

最佳答案

你在打电话

verificare(polinom.get(polinom.size() - 1), polinom,' 'polinom.size() - 1);


并得到一个值为-1的ArrayIndexOutOfBoundsException,表示在收到错误时您的polinom集合的大小为0。检查它,如果大小为0,则不要调用此方法。

07-22 11:59