You are making two fatal mistakes, from the very beginning; and this mistake drives all your development nowhere.
First mistake is quite obvious: dealing with strings as elements of an expression. I don't think it needs any explanation. In Computer Algebra Systems (CAS) expressions and calculations are often called "symbolic", and I already met some naive people thinking that "symbols" should mean characters or string. But it makes no sense. So, what is, structurally, an expression?
If you look thoroughly at the expression structure, you will see that it is a tree. In most algebras, it is a binary tree, with some nodes having only zero or one child node, which can be represented as a binary tree where "left" or "right" children (really representing left or right operand in the order they are written in usual mathematical notation), or both operand children are null.
Please see:
Binary expression tree — Wikipedia, the free encyclopedia.
То represent such tree with Java, you have to define a node class, which represent an operator, Boolean or not. The operator type is best represented as an
enum
type, but it can also be an abstract class with some virtual functions to be overridden in each concrete runtime class; this way, you could use
polymorphism. The choice depends on what you want to achieve.
Here is the conclusion: you have to make a step back and redesign everything based on trees; forget lists and string elements. Based on that, you can develop parsing of the expression string into the tree and all other operations required. The topics of programming dealing with expression trees are very popular, so you can find a lot of code samples, open-source products, tutorials, and so on:
Java: binary expression tree.
—SA