[英]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;
我在return gameBoard;
行上收到错误消息“类型不匹配无法从 String[][] 转换为 GameBoard[][]” return gameBoard;
. . How can I change this my public class GameBoard so that I am able to return string values in the 2D array?如何更改我的公共类 GameBoard 以便我能够在 2D 数组中返回字符串值? (I do not want to change the methods return type to String[][]) (我不想将方法返回类型更改为 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.发生这种情况是因为您告诉编译器您将返回GameBoard[][]
类型,但实际上返回的是String[][]
类型,您可能会因为以与命名类相同的方式命名成员变量而混淆了自己。 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.通常,您为 GameBoard 定义类的方法很好,因为您可能希望稍后对其执行不同的操作,例如move()
内容从 1 个单元格移动到另一个单元格或其他内容。 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但请记住,您的“客户端类”不知道您的类内部的外观,因此您不能将GameObject
存储为String[][]
,反之亦然
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 )这是您的类的示例,其中包含有关如何声明使用 GameBoard 的变量以及建议的一些评论:(这将无法正常工作,您必须删除不想使用的方法,因为它们在返回类型)
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.