返回2D数组

时间:2017-03-19 22:22:18

标签: java arrays 2d

我有一个创建2D数组的方法。我想返回这个2D数组,以便在另一个类中使用它。

public class drawBoxClass {
public String[] drawBox(int boxLength) {

    String[][] arrayBox = new String[boxLength][boxLength+1];

    //method stuff

    return new String[][]arrayBox;
    }
}

我已经尝试使用谷歌搜索如何返回二维字符串数组,但我不知道如何返回它。

我得到"数组维度缺失"。

1 个答案:

答案 0 :(得分:2)

您的代码存在两个问题:

(1)drawBox方法签名的返回类型应为2d数组,即String[][],您当前的方法签名只能返回单维数组

(2)return语句应该像return arrayBox(不需要再次指定变量类型)

public String[][] drawBox(int boxLength) {
    String[][] arrayBox = new String[boxLength][boxLength+1];
    //method stuff
    return arrayBox;
}
相关问题