【问题标题】:Help with updating loop condition帮助更新循环条件
【发布时间】:2011-07-28 16:38:20
【问题描述】:

好的。所以我正在尝试编写一个应用程序,以正确的操作顺序解决问题(即PEMDAS,但没有PE lol)。我可能做错了,但无论如何。我需要帮助的是,作为对数组中每个元素的迭代,如果它是一个运算符,我会删除两个元素(运算符周围的数字,因为我将运算符替换为正确的计算,所以一个包含三个元素的数组,5 +5,将成为一个包含一个元素的数组,10) 这很好,但是 for 语句条件不会更新,所以,换句话说,当我更新数组时,array.length 会变短,但是for-statement 无法识别并继续传递数组的边界,这会导致 outofbounds 错误。最好的解释方法是通过我的代码,所以这里是:

for (i = 0; i < splitEquation.length; i++){
        if (splitEquation[i].equals("*")){
            splitEquation[i] = Double.toString(Double.parseDouble(splitEquation[i - 1]) * Double.parseDouble(splitEquation[i + 1]));
            splitEquation = removeIndex(splitEquation, i - 1);
            splitEquation = removeIndex(splitEquation, i + 1);
            solution += Double.parseDouble(splitEquation[i - 1]);

        }
}

和 removeIndex():

private String[] removeIndex(String [] s, int index){
    String [] n = new String[s.length - 1];

    System.arraycopy(s, 0, n, 0, index - 1);
    System.arraycopy(s, index, n, index - 1, s.length - index);
    return n;
}

提前致谢, -埃里克

附:如果您需要澄清我的代码的作用,请告诉我 :)

【问题讨论】:

    标签: java arrays exception for-loop


    【解决方案1】:

    使用ArrayList 会让您的生活轻松很多:

    ArrayList<String> splitEquation = 
        new ArrayList<String>(Arrays.toList("5","*","5"));
    
    for (i = 0; i < splitEquation.size(); i++){
        if (splitEquation.get(i).equals("*")) {
            splitEquation.set(i, 
                (Double.toString(Double.parseDouble(splitEquation.get(i - 1)) * 
                    Double.parseDouble(splitEquation.get(i + 1))));
            splitEquation.remove(i - 1); // shortens array by 1
            splitEquation.remove(i); // this used to be (i + 1)
            i--; // move back one (your computed value)
            solution += Double.parseDouble(splitEquation.get(i));
    
        }
    }
    

    话虽这么说......你真的需要修改你的数组吗?

    编辑:问题现在更清楚了,数组正在修改,因为它需要作为一系列表达式进行评估。他还想加减法:

    递归函数是你的朋友:)

    public static double result(ArrayList<String> mySequence, double total, int index)
    {
        if (index == 0)
            total = Double.parseDouble(mySequence.get(index));
    
        if (index == (mySequence.size() - 1))
            return total;
        else if (mySequence.get(index).equals("*"))
            total *= Double.parseDouble(mySequence.get(index + 1));
        else if (mySequence.get(index).equals("/"))
            total /= Double.parseDouble(mySequence.get(index + 1));
        else if (mySequence.get(index).equals("+"))
        {
            index++;
            double start = Double.parseDouble(mySequence.get(index));
            total += result(mySequence, start, index);
            return total;
        }
        else if (mySequence.get(index).equals("-"))
        {
            index++;
            double start = Double.parseDouble(mySequence.get(index));
            total -= result(mySequence, start, index);
            return total;
        }
    
        index++;
        return result(mySequence, total, index);
    
    }
    
    public static void main(String args[])
    {
        ArrayList<String> splitEquation = 
            new ArrayList<String>(
                Arrays.asList("5","*","5","/","5","*","4","-","3","*","8"));
    
        double myResult = result(splitEquation, 0, 0); 
        System.out.println("Answer is: " + myResult);
    }
    

    输出:

    答案是:-4.0

    【讨论】:

    • 但到最后不会还是越界了吗?如果我删除它,for 语句仍将继续。修改它是我能想到的唯一方法:/ 不过谢谢!
    • 由于i--;,它不会越界——它将迭代器设置为有效元素。然后它被循环递增并与i &lt; splitEquation.size() 比较,然后导致循环退出(如果你在数组列表的末尾)
    • 原地修改数组的目的是什么?你想得到一个计算值数组吗?为什么不直接使用第二个数组作为结果?
    • 嗯,你的意思是,例如在我得到 5 + 5 之后,我把它放到另一个数组中并继续迭代?
    • 正确。您只需将结果放入第二个 ArrayList 并执行 i++ 以向前跳转,而不是删除元素并执行 i--
    【解决方案2】:

    当您可以将输入数组用作只读并创建输出 ArrayList(或更好的堆栈)时,为什么要修改输入数组?

    【讨论】:

    • 大声笑。不知道该怎么做。因为那时我必须遍历每个新数组。我还是新手 :)。
    • 您正试图从 for 循环中修改数组或集合,这是灾难的根源——除非您可以使用迭代器删除该项目。最好简单地遍历集合并在循环时从中提取信息,将其放入堆栈(或两个 - 一个用于运算符,一个用于数字),这只是另一个集合。
    【解决方案3】:

    我认为如果你使用动态结构,比如ArrayList,你应该能够解决这个问题。

    据此:

    容量是数组的大小 用于存储元素 列表。它总是至少一样大 作为列表大小。作为元素 添加到 ArrayList,它的容量 自动增长。

    this 之前的 SO 帖子,使用动态数据结构应该可以解决您的问题,我认为当您从列表中删除和添加元素时,同样的机制也适用。但是,您可能需要实施某种机制来跟踪每次更换后物品的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-26
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多