IndexOutOfBoundsException索引0大小为0

时间:2017-03-29 02:36:22

标签: java if-statement arraylist indexoutofboundsexception

我正在编写一个程序,它几乎采用了一串数字和运算符,将它们分开,然后对其进行计算。对于For循环中的每个运算符,我有四个单独的IF语句。代码编译,但给了我一个IndexOutOfBoundsException索引: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 line\nexample: java Lab8 3+13/5-16*3\n"); 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);  
}

} //

1 个答案:

答案 0 :(得分:-1)

以下代码看起来有问题。

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 loop .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);
     }

这是真正的要求吗?