【问题标题】:For java (in Android Studio). Duplicating data in a an array from another array without referencing the duplicated array.对于 java(在 Android Studio 中)。从另一个数组复制一个数组中的数据,而不引用复制的数组。
【发布时间】:2016-01-19 15:48:42
【问题描述】:

我正在尝试创建一个益智游戏,其中有一个按钮网格,单击时会减少它们的数量。这些数字保存在下面的数组中。

public int[] a = {4,4,4,4,4};
public int[] b = {4,4,4,4,4};
public int[] c = {4,4,4,4,4};
public int[] d = {4,4,4,4,4};
public int[] e = {4,4,4,4,4};
public int[] Puzzle[] = {a,b,c,d,e};

首次创建活动时,我从拼图数组中调用信息,并根据此数据数组中的数字设置视图中的对象。在视图上按下按钮时。数组中的数字减少。

我想要做的是设置一个重置按钮以将视图和此数组中的数字恢复为原始数据。创建拼图时,我尝试将数据从 Puzzle[] 复制到 savedPuzzle[] (我创建的另一个用于存储数据的数组)。当拼图第一次启动时,savePuzzle[] 正在从 Puzzle[] 复制,而当拼图重置时,Puzzle[] 正在从 savedPuzzle[] 中取回数据。

我对这个问题进行了一些研究,发现最常见的答案是使用 .clone() 函数,所以它看起来像:

savedPuzzle = Puzzle.clone()

但是 .clone() 命令对我不起作用。 system.arrarycopy() 命令和 array.copyOf() 也没有用。

这是我正在使用的代码:

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setBoard();
    savedPuzzle = Puzzle.clone();
}


// CHANGES VALUES DURING PUZZLE
public int[] a = {4,4,4,4,4};
public int[] b = {4,4,4,4,4};
public int[] c = {4,4,4,4,4};
public int[] d = {4,4,4,4,4};
public int[] e = {4,4,4,4,4};
public int[] Puzzle[] = {a,b,c,d,e};

// STORES THE SQUARE INFO FOR THE PUZZLE BEING PLAYED
public int[] V = {0,0,0,0,0};
public int[] W = {0,0,0,0,0};
public int[] X = {0,0,0,0,0};
public int[] Y = {0,0,0,0,0};
public int[] Z = {0,0,0,0,0};
public int[] savedPuzzle[] = {V,W,X,Y,Z};

// ASSIGNING BUTTON VIEWS TO ARRAY COORDINATES
final int[] R1 = {R.id.A1,R.id.B1,R.id.C1,R.id.D1,R.id.E1};
final int[] R2 = {R.id.A2,R.id.B2,R.id.C2,R.id.D2,R.id.E2};
final int[] R3 = {R.id.A3,R.id.B3,R.id.C3,R.id.D3,R.id.E3};
final int[] R4 = {R.id.A4,R.id.B4,R.id.C4,R.id.D4,R.id.E4};
final int[] R5 = {R.id.A5,R.id.B5,R.id.C5,R.id.D5,R.id.E5};
final int[] rAll[] = {R1,R2,R3,R4,R5};


public void setSquareColor(int x, int y){
    Button theView = (Button) findViewById(rAll[x][y]);
    if (Puzzle[x][y]==0){
        theView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
        theView.setText("");}
    else if(Puzzle[x][y]==1){
        theView.getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
        theView.setText("1");}
    else if(Puzzle[x][y]==2){
        theView.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
        theView.setText("2");}
    else if(Puzzle[x][y]==3){
        theView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
        theView.setText("3");}
    else if(Puzzle[x][y]==4){
        theView.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
        theView.setText("4");}
    else if(Puzzle[x][y]==5){
        theView.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
        theView.setText("5");}

}
public void resetSquareColor(int x, int y){
    Button theView = (Button) findViewById(rAll[x][y]);
    if (savedPuzzle[x][y]==0){
        theView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
        theView.setText("s");}
    else if(savedPuzzle[x][y]==1){
        theView.getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
        theView.setText("s1");}
    else if(savedPuzzle[x][y]==2){
        theView.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
        theView.setText("s2");}
    else if(savedPuzzle[x][y]==3){
        theView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
        theView.setText("s3");}
    else if(savedPuzzle[x][y]==4){
        theView.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
        theView.setText("s4");}
    else if(savedPuzzle[x][y]==5){
        theView.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
        theView.setText("s5");}}

public void resetCurrentPuzzle(View view){
    Puzzle = savedPuzzle.clone();
    for (int i=0; i<5; i++) {
        for (int j = 0; j < 5; j++) {
            resetSquareColor(i, j);
        }
    }}

public void setBoard() {
    for (int i=0; i<5; i++) {
        for (int j = 0; j < 5; j++) {
            setSquareColor(i, j);

}}}


public void DoCheck(View view){
    switch(view.getId()){
        case R.id.A1:
            ReduceOnPress(0, 0);
            setSquareColor(0, 0);
            break;
        case R.id.A2:
            ReduceOnPress(1, 0);
            setSquareColor(1, 0);
            break;
        case R.id.A3:
            ReduceOnPress(2, 0);
            setSquareColor(2, 0);
            break;
        case R.id.A4:
            ReduceOnPress(3, 0);
            setSquareColor(3, 0);
            break;
        case R.id.A5:
            ReduceOnPress(4, 0);
            setSquareColor(4, 0);
            break;
        case R.id.B1:
            ReduceOnPress(0, 1);
            setSquareColor(0, 1);
            break;
        case R.id.B2:
            ReduceOnPress(1, 1);
            setSquareColor(1, 1);
            break;
        case R.id.B3:
            ReduceOnPress(2, 1);
            setSquareColor(2, 1);
            break;
        case R.id.B4:
            ReduceOnPress(3, 1);
            setSquareColor(3, 1);
            break;
        case R.id.B5:
            ReduceOnPress(4, 1);
            setSquareColor(4, 1);
            break;
        case R.id.C1:
            ReduceOnPress(0, 2);
            setSquareColor(0, 2);
            break;
        case R.id.C2:
            ReduceOnPress(1, 2);
            setSquareColor(1, 2);
            break;
        case R.id.C3:
            ReduceOnPress(2, 2);
            setSquareColor(2, 2);
            break;
        case R.id.C4:
            ReduceOnPress(3, 2);
            setSquareColor(3, 2);
            break;
        case R.id.C5:
            ReduceOnPress(4, 2);
            setSquareColor(4, 2);
            break;
        case R.id.D1:
            ReduceOnPress(0, 3);
            setSquareColor(0, 3);
            break;
        case R.id.D2:
            ReduceOnPress(1, 3);
            setSquareColor(1, 3);
            break;
        case R.id.D3:
            ReduceOnPress(2, 3);
            setSquareColor(2, 3);
            break;
        case R.id.D4:
            ReduceOnPress(3, 3);
            setSquareColor(3, 3);
            break;
        case R.id.D5:
            ReduceOnPress(4, 3);
            setSquareColor(4, 3);
            break;
        case R.id.E1:
            ReduceOnPress(0, 4);
            setSquareColor(0, 4);
            break;
        case R.id.E2:
            ReduceOnPress(1, 4);
            setSquareColor(1, 4);
            break;
        case R.id.E3:
            ReduceOnPress(2, 4);
            setSquareColor(2, 4);
            break;
        case R.id.E4:
            ReduceOnPress(3, 4);
            setSquareColor(3, 4);
            break;
        case R.id.E5:
            ReduceOnPress(4, 4);
            setSquareColor(4, 4);
            break;
    }
}

public void ReduceOnPress(int x, int y){
    if (Puzzle[x][y] > 0){
        Puzzle[x][y] -= 1;
    }
}

【问题讨论】:

  • 作为sidenode,我宁愿写int[][],这样更易​​读,而不是用括号分隔类型和变量名。
  • 如果您正在寻找 memcpy() 等价物,this 是最好不过了...
  • “没有工作”以什么方式?

标签: java android arrays


【解决方案1】:

我最终通过克隆数组 (a[],b[],c[],d[],e[]) 中的数组而不是克隆数组数组(Puzzle[][] )。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2022-01-14
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    相关资源
    最近更新 更多