索引越界异常索引 0 大小 0



我正在编写一个程序,它几乎是一串数字和运算符,将它们分开,然后对其进行计算。对于 For 循环中的每个运算符,我有四个单独的 IF 语句。代码编译,但给了我一个 IndexOutOfBoundsException Index:0尺寸:0在第 70 行和第 28 行。有人对为什么会发生这种情况有任何建议吗?谢谢。

import java.util.*;
import java.io.*;
public class Lab8
{
public static void main( String[] args)
{
    if ( args.length<1) { System.out.println("FATAL ERROR: Missing expression on command linenexample: java Lab8 3+13/5-16*3n"); System.exit(0); }
    String expr= args[0];  // i.e. somethinig like "4+5-12/3.5-5.4*3.14"; 
    System.out.println( "expr: " + expr );
    ArrayList<String> operatorList = new ArrayList<String>();
    ArrayList<String> operandList = new ArrayList<String>();
    StringTokenizer st = new StringTokenizer( expr,"+-*/", true );
    while (st.hasMoreTokens())
    {
        String token = st.nextToken();
        if ("+-/*".contains(token))
            operatorList.add(token);
        else
            operandList.add(token);
        }
    System.out.println("Operators:" + operatorList);
    System.out.println("Operands:" + operandList);
    double result = evaluate( operatorList, operandList );
    System.out.println("The expression: " + expr + " evalutes to " + result + "n");
} // END MAIN


static double evaluate( ArrayList<String> operatorList, ArrayList<String> operandList)
{
    String operator;
    double result;
    ArrayList<Double> andList = new ArrayList<Double>();
    for( String op : operandList )
    {
        andList.add( Double.parseDouble(op) );
    }
    for(int i=0;i<operatorList.size();++i)
    {
        if(operatorList.get(i).equals("*"))
        {
        operator = operatorList.get(i);
        }
            result = andList.get(i) * andList.get(i+1); 
            andList.set(i,result); 
        //operandList.set(i,result);
            andList.remove(i+1);
            operatorList.remove(i);

        if(operatorList.get(i).equals("/"))
        {
            operator = operatorList.get(i);
        }
            result = andList.get(i) / andList.get(i+1); 
            andList.set(i,result); 
            andList.remove(i+1);
            operatorList.remove(i);

        if(operatorList.get(i).equals("+"))
        {
            operator = operatorList.get(i);
        }   
            result = andList.get(i) + andList.get(i+1);
            andList.set(i,result);
            andList.remove(i+1);
            operatorList.remove(i); 

        if(operatorList.get(i).equals("-"))
        {
            operator = operatorList.get(i);
        }
            result = andList.get(i) - andList.get(i+1);
            andList.set(i,result);
            andList.remove(i+1);
            operatorList.remove(i);
    }
    return andList.get(0);  
}

}//

看起来下面的代码有问题。

if(operatorList.get(i).equals("*"))
            {
                operator = operatorList.get(i);
            }
            result = andList.get(i) * andList.get(i+1);
            andList.set(i,result);
            //operandList.set(i,result);
            andList.remove(i+1);
            operatorList.remove(i);

            if(operatorList.get(i).equals("/"))
            {
                operator = operatorList.get(i);
            }
            result = andList.get(i) / andList.get(i+1);
            andList.set(i,result);
            andList.remove(i+1);
            operatorList.remove(i);

            if(operatorList.get(i).equals("+"))
            {
                operator = operatorList.get(i);
            }
            result = andList.get(i) + andList.get(i+1);
            andList.set(i,result);
            andList.remove(i+1);
            operatorList.remove(i);

            if(operatorList.get(i).equals("-"))
            {
                operator = operatorList.get(i);
            }
            result = andList.get(i) - andList.get(i+1);
            andList.set(i,result);
            andList.remove(i+1);
            operatorList.remove(i);

所有 if 循环都有相同的代码集

result = andList.get(i) * andList.get(i+1);
                andList.set(i,result);
                //operandList.set(i,result);
                andList.remove(i+1);
                operatorList.remove(i);

这应该是前一个 if 块的一部分。这里发生的事情是,当代码到达第 70 行时,它已经从以前的 operatorList.remove 语句中的 operatorList 中删除了元素。您需要通过确保代码是否属于 if 循环来更正它 .e.g. 对于第一个 if 循环 -

    if(operatorList.get(i).equals("*"))
    {
        operator = operatorList.get(i);
        result = andList.get(i) * andList.get(i+1);
        andList.set(i,result);
        //operandList.set(i,result);
        andList.remove(i+1);
        operatorList.remove(i);
     }

这是真正的要求吗?

最新更新