简体   繁体   English

2d对象数组的深层副本

[英]Deep copies of 2d object array

how can I make on a button press a new deep copy of a 2 dimensional array? 如何按下按钮按下二维数组的新深层副本?

Basically I created a game field with buttons. 基本上我用按钮创建了一个游戏区域。 The game is called sokoban and it's a puzzle. 这个游戏叫做推箱子,这是一个谜题。 The player is moving from one button to the other with arrow keys on a fixed map (8x8 buttons). 播放器在固定地图上使用箭头键从一个按钮移动到另一个按钮(8x8按钮)。 I want to implement an undo function. 我想实现一个撤销功能。 So I thought that I just create a deep copy of the JButton array before each move and save it into a stack. 所以我认为我只是在每次移动之前创建JButton数组的深层副本并将其保存到堆栈中。 So when I press the undo button it calls the pop function of my stack. 因此,当我按下撤销按钮时,它会调用我的堆栈的弹出功能。 The problem is that I need to declare and initialize another JButton[][] where I can save the game field to before each move. 问题是我需要声明并初始化另一个JButton [] [],我可以在每次移动之前保存游戏字段。 Since I want infinite possible moves and also undos it seems impossible to me. 因为我想要无限可能的动作而且还有动词,这对我来说似乎是不可能的。 I can't declare and initalize infite diffrent JButton[][] arrays. 我不能声明和初始化不同的不同JButton [] []数组。 Any idea on how I can solve that? 关于如何解决这个问题的任何想法?

That's how I copy a 2d object array: 这就是我复制2d对象数组的方法:

    JButton[][] tempArray = new JButton[jbArray.length][jbArray[0].length];

    for (int i = 0; i < getJbArray().length; i++) {
        for (int j=0;j<getJbArray()[0].length;j++) {
            tempArray[i][j]=jbArray[i][j];
        }
    } 

    movesStack.push(tempArray);

Unfortunately you can't clone swing components in general, as they do not implement the Cloneable interface. 遗憾的是,您无法克隆swing组件,因为它们没有实现Cloneable接口。 As I see it you have two options: 在我看来,你有两个选择:

  1. Create a new JButton inside your double loop and copy whatever properties (like alignment, color etc.) you have set to the new JButton 在双循环中创建一个新的JButton并复制你设置到新JButton的任何属性(如对齐,颜色等)

  2. Write your own class that extends JButton and implement the Cloneable interface 编写自己的类,扩展JButton并实现Cloneable接口

The first way is somewhat of a hack and not very robust or reusable. 第一种方式是某种程度的黑客攻击,而不是非常强大或可重用。 The second way is much better practice. 第二种方式是更好的练习。 In this case you'll have to define how the deep copy is supposed to happen, and ensure that all relevant properties are copied over. 在这种情况下,您必须定义深层副本应该如何发生,并确保复制所有相关属性。

You've got the right idea. 你有正确的想法。 You're not quite going deep enough. 你还不够深入。

    public JButton[][] copy(JButton[][] jbArray) {
        JButton[][] tempArray = new JButton[jbArray.length][jbArray[0].length];

        for (int i = 0; i < jbArray.length; i++) {
            for (int j = 0; j < jbArray[0].length; j++) {
                tempArray[i][j] = new JButton(jbArray[i][j].getText());
            }
        }

        return tempArray;
    }

Rather than copying JButtons, you should have a model that you use to set the JButtons. 您应该拥有一个用于设置JButton的模型,而不是复制JButton。 Maybe a ModelClass[][] array? 也许是ModelClass[][]数组?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM