我如何解决数组超出范围的异常? [重复]

问题描述 投票:0回答:1

我正在尝试从字符串数组中提取系数和指数。例如:“ x ^ 2 + 2x + 1”或“ x ^ 5”或“ x ^ 2 -x + 1”。我在此多项式构造函数中编写的代码在大多数情况下都适用,但是当字符串的长度为1时,我得到了索引超出范围的错误:“线程“ main”中的异常” java.lang.ArrayIndexOutOfBoundsException:索引1超出了范围长度1”。我不确定如何解决此问题。谁能帮我这个代码?

如果数组的长度为1,我试图打破while循环。

public class Polynomial extends PolynomialInterface {
    public Polynomial(String s) {
        // complete this code
        Term newTerm;
        DList<Term> dList = new DList<Term>();

        int exp = 0;
        double coef = 0;

        s = s.replaceAll(" ", "");



        String[] array = s.split("\\+|(?=-)");


       for(String i : array){
            System.out.println("split: " + i);
        }


        for (int i = 0; i <= array.length; i++) {

                while (array[i].contains("x")) {

                    exp = 0;

                    if ((array[i].substring(0, 1).equalsIgnoreCase("x"))) {
                        coef = 1;
                    }


                    if (array[i].substring(0, 1).equals("-") && array[i].substring(1, 2).equalsIgnoreCase("x")) {
                        coef = -1;
                        exp = 1;
                    }


                    if (array[i].contains("^")) {

                        int index = array[i].indexOf("^");
                        String myString = "";

                        if (index != -1) {
                            myString = array[i].substring(index + 1, index + 2);
                            if (!myString.isEmpty()) {
                                exp = Integer.parseInt(myString);
                            }
                        }

                    }

                    if (!array[i].contains("^")) {
                        exp = 1;
                    }




                    int end = array[i].indexOf("x");

                    String subString = "";

                    if (end != -1) {
                        subString = array[i].substring(0, end);
                        if (!subString.isEmpty() && !subString.contains("-")) {
                            coef = Double.parseDouble(subString);
                        } else if (!subString.isEmpty() && !subString.equals("-")) {
                            coef = Double.parseDouble(subString);

                        }
                    }


                    newTerm = new Term(coef, exp);
                    dList.addLast(newTerm);

                    if(array.length==1){
                        break;
                    }
                    i++;

                }

                while (array[i].matches("([-]\\d*|\\d*)") && (i < array.length)) {


                    coef = Double.parseDouble(array[i]);
                    exp = 0;

                    newTerm = new Term(coef, exp);
                    dList.addLast(newTerm);


                    break;

                }
            }








        //super.data = dList;



        }

当我调试时,我得到了正确的系数和指数,然后进行了迭代,并且得到了超出范围的索引错误

java arrays indexoutofboundsexception
1个回答
2
投票
for (int i = 0; i <= array.length; i++)

您的for循环转到array.length,这导致空指针异常。请记住,在Java数组中,数组的起始位置为0,数组的长度为-1。因此,您的竞争条件应该是我

© www.soinside.com 2019 - 2024. All rights reserved.