简体   繁体   中英

Returning a 2D Array from a method using the class name as the input

I'm receiving the error message "type mismatch cannot convert from String[][] to GameBoard[][]" on the line return gameBoard; . How can I change this my public class GameBoard so that I am able to return string values in the 2D array? (I do not want to change the methods return type to String[][])


public class GameBoard {

    String[][] gameBoard;


    /**
     * Constructor
     */

    public GameBoard() {
        gameBoard = new String[4][5];
    }   

    /**
     * Filling the playable board with Strings
     * @return
     */

     public GameBoard[][] fillGameBoard() {

        gameBoard[0][0] = new String("1");
        gameBoard[0][1] = new String("2");
        gameBoard[0][2] = new String("3");
        gameBoard[0][3] = new String("4");
        gameBoard[0][4] = new String("5");
        gameBoard[1][0] = new String("6");
        gameBoard[1][1] = new String("7");
        gameBoard[1][2] = new String("8");
        gameBoard[1][3] = new String("9");
        gameBoard[1][4] = new String("10");
        gameBoard[2][1] = new String("11");
        gameBoard[2][2] = new String("12");
        gameBoard[2][3] = new String("13");
        gameBoard[3][1] = new String("14");
        gameBoard[3][2] = new String("15");
        gameBoard[3][3] = new String("16");



        return gameBoard;

    }```


That happens because you tell the compiler you are going to return a GameBoard[][] type but are actually returning a String[][] type, you probably confused yourself by naming your member variable the same way you named your class. You probably want to come up with a less confusing name for it.

Generally your approach of defining a class for your GameBoard is fine, since you probably want to perform different actions on it later on like, move() a content from 1 cell to another or something else. But remember, that your 'client-class' doesn't know about how the interior of your class looks and thus you cannot just store a GameObject as String[][] and vice versa

Here is an example of your class with some comments on how to declare variables that use your GameBoard along with suggestions: ( This will not work as is, you have to remove the methods you do not want to use, since they are not matching in return types )

public class GameBoard {

    String[][] gameBoard;

    /**
     * Constructor
     */
    public GameBoard() {
        gameBoard = new String[4][5];
    }   

    // Approach 1
     public void fillGameBoard() {

        gameBoard[0][0] = "1";
        gameBoard[0][1] = "2";
        gameBoard[0][2] = "3";
        gameBoard[0][3] = "4";
        gameBoard[0][4] = "5";
        gameBoard[1][0] = "6";
        gameBoard[1][1] = "7";
        gameBoard[1][2] = "8";
        gameBoard[1][3] = "9";
        gameBoard[1][4] = "10";
        gameBoard[2][1] = "11";
        gameBoard[2][2] = "12";
        gameBoard[2][3] = "13";
        gameBoard[3][1] = "14";
        gameBoard[3][2] = "15";
        gameBoard[3][3] = "16";
    }

    // Approach 2
    // If you really want to go with the way you had your declaration set up
    // GameBoard board = new GameBoard().fillGameBoard();
    // you would have to do this
    public GameBoard fillGameBoard() {
        fillGameBoard();

        return this;
    }

    // Approach 3
    // If you want to do this, you don't need your class at all and your fillGameBoard 
    // has to return a String[][] type. I would recommend against it though,
    // since you are using a highly object oriented language as Java
    public String[][] fillGameBoard() {
        fillGameBoard();

        return gameBoard;
    }

    public static void main(String[] args) {
        // Approach 1
        // I recommend this approach!
        GameBoard board2 = new GameBoard();
        board2.fillGameBoard();

        // Approach 2
        GameBoard board = new GameBoard().fillGameBoard();

        // Approach 3
        String[][] board3 = new GameBoard().fillGameBoard();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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