意外的toString结果

时间:2015-04-24 02:18:16

标签: java

所以我将值添加到哈希映射中,然后打印出哈希映射。我的哈希映射的值似乎打印得很好,但由于某种原因,我打印的键与我输入的键不同。 以下是相关代码:

//adds values into the hash map
public void addTransition(int curState, char curBit, int newState, char newBit, int direction){
   Trigger trigger = new Trigger(newState, newBit);
   Action action = new Action(newState, newBit, direction);
   program.put(trigger,action);
   System.out.println(program);
}

编辑:更新了触发器类

//the trigger class
class Trigger{
    public char curBit;
    public int curState;
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + getOuterType().hashCode();
        result = prime * result + curBit;
        result = prime * result + curState;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Trigger other = (Trigger) obj;
        if (!getOuterType().equals(other.getOuterType()))
            return false;
        if (curBit != other.curBit)
            return false;
        if (curState != other.curState)
            return false;
        return true;
    }
    private TuringMachine getOuterType() {
        return TuringMachine.this;
    }
    public Trigger(int x, char y){
        curState = x;
        curBit = y;
    }
    @Override
    public String toString(){
        return "|" + curState + ", " + curBit + "|";
    }
}

例如,如果我执行addTransition(0,'1',0,'1',1),则会打印出{(0, 49)=(0, 1, 1)}

修改:因此对于上述内容,它会按预期打印(0,1) = (0,1,1) 但是,如果我在添加之前的addTransition(0,'0',1,'0',1)之后0,1,0,1,1 {|0, 1|=(0, 1, 1), |1, 0|=(1, 0, 1)} 它给了我: DATA _NULL_; call symputx("Today", Put(datetime(),mysdt.)); run;

1 个答案:

答案 0 :(得分:4)

您正在将char传递给一个带int的构造函数,并且它会被隐式转换。

'1'的字符代码为49