复制对象而不更改原始对象 - java

时间:2014-12-05 20:54:13

标签: java clone copy-constructor tic-tac-toe

我正在做一个Tic-Tac-Toc游戏,我有一个方法可以从当前状态返回所有可能的动作。我正在尝试重新获取一个对象并将其复制,更改并将其输入到数组列表中。

@Override
public ArrayList<State> getAllPossibleMoves(State current) {
    ArrayList<State> moves = new ArrayList<State>();
    for (int i=0; i<current.getState().length; i++){
        for (int j=0; j<current.getState().length; j++){
            if (current.getState(i, j) == '-'){
                char[][] tmp1;
                tmp1 = current.getState().clone();
                tmp1[i][j] = 'O';
                TicTacToeState tmp2 = new TicTacToeState(tmp1);
                tmp2.setState(tmp1);
                moves.add(tmp2);
                //current.setState(i, j, '-');
            }
        }
    }
    return moves;
}

无论我尝试什么 - 我对tmp1对“当前”的影响做了任何改变。 我尝试了clone()和复制构造函数。只是提到类“State”是抽象的,“TicTacTicState”扩展为“State”。

了解更多信息 - 这是类State

公共抽象类State实现Cloneable {

protected char[][] state;
protected int evaluation;   


@Override
public Object clone() throws CloneNotSupportedException {
    // TODO Auto-generated method stub
    return super.clone();
}

@Override
public boolean equals(Object obj) {
    return state.equals(((State)obj).getState());
    //return super.equals(obj);
}

public State(){

}

public State(State s){
    this.state = s.state;
    this.evaluation = s.evaluation;
}

public State(char[][] state){
    this.state = state;
    this.evaluation = 0;
}

public State(char[][] state, int evaluation){
    this.state = state;
    this.evaluation = evaluation;

}

public char[][] getState(){
    return this.state;
}

public char getState(int row, int column) {
    return state[row][column];
}


public void setState(char[][] state) {
    this.state = state;
}

public void setState(int row, int col, char player){
    this.state[row][col] = player;
}
public abstract int getEvaluation(State state);

public abstract boolean isStateFull(State current); //returns true is it's a "terminal node"

public abstract ArrayList<State> getAllPossibleMoves(State current);

}

1 个答案:

答案 0 :(得分:0)

您必须亲自复制阵列。您可以使用java.lang.System.arraycopy()执行此操作:

import java.util.Arrays;

public class ArrayCopy {

    public static void main(String[] args) {

        // set up an empty source array
        char[][] src = new char[5][3];

        // fill it with random digits
        for(int i = 0 ; i < src.length; i++) {
            for(int j = 0 ; j < src[0].length; j++) {
                src[i][j] = (char) (Math.random() * 10 + 48);
            }
        }

        // show what it looks like
        printArray(src);

        // create an empty destination array with the same dimensions
        char[][] dest = new char[src.length][src[0].length];

        // walk over array and copy subarrays using arraycopy
        for (int i = 0; i < src.length; i++) {
            System.arraycopy(src[i], 0, dest[i], 0, src[0].length);
        }

        // make a change to the copy
        dest[0][0] = 'X';

        // the source array is still the same
        printArray(src);

        // hey presto!
        printArray(dest);
    }

    private static void printArray(char[][] array) {
        for(int i = 0 ; i < array.length; i++) {
            System.out.println(Arrays.toString(array[i]));
        }
        System.out.println();
    }
}   

示例输出:

[5, 5, 9]
[0, 4, 7]
[4, 8, 6]
[1, 5, 4]
[3, 9, 3]

[5, 5, 9]
[0, 4, 7]
[4, 8, 6]
[1, 5, 4]
[3, 9, 3]

[X, 5, 9]
[0, 4, 7]
[4, 8, 6]
[1, 5, 4]
[3, 9, 3]