Ich erstelle eine Postfix-Evaluator-Methode mit einer selbst implementierten Linked List und ich bin wirklich am Ende hier stecken. Ich muss den endgültigen Stack-Wert per Pop zurückgeben. In meiner Methode muss mein Rückgabetyp eine Ganzzahl sein. Ich habe versucht, eine Lösung und es sagt mir, "kann nicht von Operand<Integer>
zu Integer
konvertieren". Ich nehme an, ich muss es irgendwie konvertieren, aber ich weiß nicht, wie ich das anstellen soll, jede Hilfe wäre willkommen.Converting Operand <T> zu Integer
Ich konnte nichts hilfreich finden, also entschuldige ich mich, wenn dies ein Duplikat ist.
Hier ist meine Methode:
public class ArithPostfixEvaluator implements PostfixEvaluator<Integer> {
private final StackInterface<Operand<Integer>> stack;
/**
* Constructs an {@link ArithPostfixEvaluator}
*/
public ArithPostfixEvaluator(){
//Initialize the LinkedStack
stack = new LinkedStack<>();
}
public Integer evaluate(String expr) throws IllegalPostfixExpressionException {
ArithPostfixParser parser = new ArithPostfixParser(expr);
for (Token<Integer> token : parser) {
Type type = token.getType();
switch(type){
case OPERAND:
//What to do when we see an operand?
//create an operand variable
Operand<Integer> operand = token.getOperand();
//push the operand onto the stack
stack.push(operand);
break;
case OPERATOR:
//What to do when we see an operator?
//create an operator variable
Operator<Integer> operator = token.getOperator();
//make a new operand called result
Operand<Integer> result;
//pop 2 operands
Operand<Integer> op1 = stack.pop();
Operand<Integer> op2 = stack.pop();
//the first operand goes in the second position
operator.setOperand(2, op1);
operator.setOperand(1, op2);
//perform operation on result
result = operator.performOperation();
//push the result back onto the stack
stack.push(result);
break;
default:
throw new IllegalStateException("Parser returned an invalid Type: " + type);
}
}
// what to return?
///////////PROBLEM AREA////////////////
Integer Finalval = stack.pop();
//pop the remaining element on the stack
return Finalval ;
}
Hier ist meine verkettete Liste:
public class LinkedStack<T> implements StackInterface<T> {
private LLNode<T> head;
private int size;
public T pop() throws StackUnderflowException {
if(isEmpty()) throw new StackUnderflowException("Stack Underflow yo");
T temp = head.getData();
head = head.getNext();
return temp;
}
public T top() throws StackUnderflowException {
if(isEmpty()) throw new StackUnderflowException("Stack Underflow yo");
return head.getData();
}
public boolean isEmpty() {
return (head == null);
}
public int size() {
return size;
}
public void push(T elem) {
LLNode<T> newnode = new LLNode<T>(elem);
newnode.setNext(head);
head = newnode;
size++;
}
}
Und mein Operand und Operator Klassen:
public class Operand<T> {
private final T value;
public Operand(T value){
this.value = value;
}
public T getValue(){
return value;
}
public String toString() {
return value.toString();
}
}
public interface Operator<T> {
public int getNumberOfArguments();
public Operand<T> performOperation();
public void setOperand(int position, Operand<T> operand);
}