1

I can't pass the 1d array into the method because there is a 2d array parameter. I can't remove the 2d array parameter because my 2048 game board runs on that 2d array board subject. Is there a workaround? I want the slideUp to work on temp and still keep my 2d array parameter board.

public void slideUp(int[][] board) {
    for (int column = 0; column < board[0].length; column++) {
        int[] temp = getCol(board, column);
        slideUp(temp);
        for (int i = 0; i < board[0].length; i++) {
            board[i][column] = temp[i];
        }
    }
}

Error code: incompatible types: int[] cannot be converted to int[][] slideUp(temp);

2 Answers2

1

As a workaround, you can create a new method that accepts int[], then represent a 1d array as a 2d array of a single row, and call the old method, something like this:

public void callSlideUp(int[] row) {
    // a 2d array of a single row
    slideUp(new int[][]{row});
}

public void slideUp(int[][] board) { ... }
0

You could overload the method by creating another method with the same name, but using a 1d array as a parameter.

public void slideUp(int[] board) {
    //your code here
}

You would have to change this to handle a 1d array.

 board[i][column]
Ben Alan
  • 1,399
  • 1
  • 11
  • 27