Okay. So I'm trying to write an applicaton that solves problems in the correct order of operations (i.e. PEMDAS, but without the PE lol). I'm probably doing this wrong, but whatever. What I need help on is that as a iterate through each element in the array, if it is an operator, I remove two elements(the numbers surrounding the operator because I replace the operator the correct computation, so an array with three elements, 5+5, will become an array with one element, 10) which would be fine, but the for-statement condition doesn't update, so, in other words, when i update the array, the array.length gets shorter, but the for-statement doesn't recognize that and continues on pass the bounds of the array, this causes outofbounds errors. Best way to explain is by my code, so here it is:
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]);
}
}
And 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);
retur开发者_如何学JAVAn n;
}
Thanks in advance, -Eric
P.S. Let me know if you need any clarification of what my code does :)
Using an ArrayList
Would make your life a whole lot easier:
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));
}
}
That being said ... do you really need to modify your array in place?
EDIT: The question is now more clear, the array was being modified because it needs to be evaluated as a series of expressions. He also wants to add addition and subtraction:
Recursive functions are your friend :)
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);
}
Output:
Answer is: -4.0
Why modify the input array when you could use the input array as read-only and create output ArrayLists (or better stacks)?
I think that if you use a dynamic structure, such as an ArrayList, you should be able to fix that problem.
According to this:
The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.
and this previous SO post, using a dynamic data structure should solve your problem, I think that the same mechanism applies for when you remove, as well as add, elements from the list. You might however, need to implement some mechanism to keep track of where the items are after each and every replacement you make.
精彩评论