물리적 배열을 직접 복사
공공 추상 클래스 상태이 복제 {
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);
}를 구현하는 클래스 상태입니다. 당신은 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]
당신이 또한 클래스의 코드를 게시 할 수있는 좋은 아이디어라고 생각하지를? –