anyset
anyset

Reputation: 43

Return 2D Array

I have a method which creates a 2D array. I want to return this 2D array to use it in another class.

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

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

    //method stuff

    return new String[][]arrayBox;
    }
}

I've tried googling on how to return 2D string arrays, but I don't get how to return it.

I'm getting "array dimension missing".

Upvotes: 1

Views: 7323

Answers (1)

Vasu
Vasu

Reputation: 22422

There are two problems with your code:

(1) Return type of drawBox method signature should be 2d array i.e., String[][], your current method signature can only return single dimensional array

(2) return statement should be like return arrayBox (no need to specify the variable type again)

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

Upvotes: 2

Related Questions