Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 相同的目标,不同的结果_Java_Queue - Fatal编程技术网

Java 相同的目标,不同的结果

Java 相同的目标,不同的结果,java,queue,Java,Queue,因此,我有一个通用对象状态,尽管没有被编辑,但它的值显然发生了变化。这就是我的意思——通过调用队列中的相同元素,我得到了不同的结果 System.out.println(mapString(que.element().getMapState())); System.out.println(que.element()); 结果: 第二个结果是正确的。 缺少mapString方法仅打印表char[][]: private static String mapString(char[][] map)

因此,我有一个通用对象
状态
,尽管没有被编辑,但它的值显然发生了变化。这就是我的意思——通过调用
队列中的相同元素,我得到了不同的结果

System.out.println(mapString(que.element().getMapState()));
System.out.println(que.element());
结果:

第二个结果是正确的。

缺少
mapString
方法仅打印表
char[][]

private static String mapString(char[][] map) {
    String mapString = "";
    for(int k = 0; k<map.length; k++) {
        for(int j = 0; j<map[k].length; j++) {
            mapString += map[k][j];
        }
        mapString += "\n";
    }
    return mapString;
}
队列
队列
的类型,其中
状态
是我的通用类型。我可以通过
toString()
方法打印它,这将产生第二个(正确的)结果

其中,
mapString
是一个
String
变量,该变量在
状态
类构造函数中使用相同的
String mapString(char[]]map)
方法启动

public State(PlayerAddress player, HashMap<String, BoxAddress> boxList, char[][] map, String solution, String stateHash) {
    this.player = player;
    this.boxList = boxList;
    this.map = map;
    this.solution = solution;
    this.stateHash = stateHash;
    this.boxListString = boxListToString(boxList);
    this.mapString = mapString(map);
}
public状态(PlayerAddress播放器、HashMap框列表、char[][]映射、字符串解决方案、字符串状态哈希){
this.player=player;
this.boxList=boxList;
this.map=map;
这个解决方案=解决方案;
this.stateHash=stateHash;
this.boxListString=boxListToString(boxList);
this.mapString=mapString(map);
}

但是,在代码的后面部分,我没有编辑
队列中的对象,但是它会编辑自己。原因可能是什么?如果这会改变什么,我可以提供完整的代码。

也许我们需要更多的代码。两点:

  • 方法
    mapString(char[]]map)
    在哪里声明?您的列表将其显示为
    私有静态…
    ,因此它可能在State类中。但是,您的代码:

    System.out.println(映射字符串(que.element().getMapState())
    
    System.out.println(que.element())

  • 似乎指示从状态类外部调用
    mapString()
    ,好像有第二个版本

  • 尝试临时更改
    toString()
    以调用
    mapString()
    方法,然后重试
  • 除此之外,请显示所有代码


    更新:谢谢你的代码。看了之后,我唯一的想法是Jorn Vernee在他早期的回答中走在了正确的轨道上。尽管状态中的构造函数包括:

    this.map=map.clone()

    这只是一个浅层克隆,由于未克隆内部级别阵列,因此几乎没有效果。你能试试深层克隆吗?i、 e.在建造商中:

    char[][] newMap = map.clone();
    for (int i = 0; i < map.length; i++) {
       newMap[i] = map[i].clone();
    }
    this.map = newMap;
    
    char[]newMap=map.clone();
    对于(int i=0;i
    出于某种原因,如果您这样做
    char[][]temp\u map=que.element().getMapState()
    ,然后在代码中编辑
    temp\u map
    ,队列中的第一个元素将更改其值


    但老实说,这没有任何意义!有人能解释一下吗?

    在将映射传递给构造函数后,您是否正在修改它?@jornverne我没有。这是我的猜测,因为您正在保存对数组的引用。改为执行
    this.map=map.clone()
    仍然值得一试。
    clone
    只是一个浅拷贝,而不是一个深拷贝,因此
    clone
    不是您想要的
    char[][
    。看这个答案这里是整个代码1。我两者都有。在主类中,它只是用于调试。他们都是私人的,所以这不应该是个问题。无论如何,它们不会改变对象,只是打印它们。2.不确定您的意思。
    temp\u map
    在内部是一个引用,如果您愿意,它是指向空闲存储中实际数组对象的指针。存储在
    que
    中的东西也是指针。赋值复制指针,但两个指针仍指向空闲存储中的同一对象。
    public State(PlayerAddress player, HashMap<String, BoxAddress> boxList, char[][] map, String solution, String stateHash) {
        this.player = player;
        this.boxList = boxList;
        this.map = map;
        this.solution = solution;
        this.stateHash = stateHash;
        this.boxListString = boxListToString(boxList);
        this.mapString = mapString(map);
    }
    
    char[][] newMap = map.clone();
    for (int i = 0; i < map.length; i++) {
       newMap[i] = map[i].clone();
    }
    this.map = newMap;