为我的地图打印乱码

时间:2015-04-23 23:31:54

标签: java hashmap

所以我有一个地图,其中键为Trigger,值为Action。触发器是一个内部类,它接受两个参数。 Action也是一个内部类,它接受三个参数。我有一个addTransition方法,它接受5个参数,前两个用于触发,后三个用于操作。在这个方法中我添加了一个system.out.println(map);但出于某种原因,它打印出来:

{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0900=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08ff=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0900=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08ff=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0900=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08ff=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0900=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0920=meta.TuringMachine$Action@ec636273}

编辑:似乎我的newBit是空的。这也是我在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);
       }
TuringMachine a = new TuringMachine();
           a.addTransition(0,'1',0,'1',1);

出于某种原因,这是打印

{(0, 49)=(0, , 0)}

1 个答案:

答案 0 :(得分:0)

您没有覆盖toString()Trigger中的Action方法,因此当您尝试从其中一个类打印对象时,它会调用Object.toString()

来自documentation

  

类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”和对象的哈希码的无符号十六进制表示组成。换句话说,此方法返回一个等于值的字符串:

getClass().getName() + '@' + Integer.toHexString(hashCode())

你应该做的是这样的事情:

class Trigger {
    private int state;
    private char curBit;

    // rest of the code

    @Override
    public String toString() {
        return "(" + this.state + ", " + this.curBit + ")";
    }
}