2017-03-19 2 views
1

私は2D配列を作成するメソッドを持っています。この2D配列を別のクラスで使用するために戻したいと思います。2D配列を返します

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

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

    //method stuff 

    return new String[][]arrayBox; 
    } 
} 

私は2D文字列配列を返す方法についてグーグルで試みましたが、返す方法はありません。

「配列の次元がありません」と表示されています。

+0

可能な重複してはなりませんing](http://stackoverflow.com/questions/20519100/java-how-to-return-in-a-method-multidimensional-array-without-aliasing) – Oghli

答えて

2

コードを持つ2つの問題がある:

(1)drawBoxメソッドシグネチャの戻り型が2次元配列、すなわち、String[][]、現在のメソッドのシグネチャであるべきであるのみ(2)1次元アレイ

を返すことができエイリアスなしの方法多次元配列で返す方法 - return文は(再び変数の型を指定する必要)return arrayBoxのように[ジャワの

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