首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Operand<T>转换为整数

将Operand<T>转换为整数
EN

Stack Overflow用户
提问于 2017-03-04 07:31:52
回答 1查看 378关注 0票数 1

我正在使用一个自实现的链表创建一个Postfix赋值器方法,我真的被困在这里了。我需要通过pop返回最终的堆栈值。在我的方法中,我的返回类型必须是Integer。我尝试了一个解决方案,它告诉我“无法从Operand<Integer>转换为Integer”。我想我需要以某种方式转换它,但我不知道如何去做,任何帮助都将不胜感激。

我找不到任何有用的东西,所以如果这是一个复制品,我道歉。

下面是我的方法:

代码语言:javascript
复制
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 ;
    }

这是我的链表:

代码语言:javascript
复制
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++;
    }

}

以及我的操作数和运算符类:

代码语言:javascript
复制
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);

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-04 07:36:27

代码语言:javascript
复制
private final StackInterface<Operand<Integer>> stack;

堆栈包含Operand<Integer>%s,因此您不能这样做:

代码语言:javascript
复制
Integer Finalval = stack.pop();

它尝试在Integer变量中存储Operand<Integer>。你可以这样做:

代码语言:javascript
复制
Integer Finalval = stack.pop().getValue();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42590174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档