2017-03-19 1 views
1

J'ai une méthode qui crée un tableau 2D. Je veux retourner ce tableau 2D pour l'utiliser dans une autre classe.Retour 2D Array

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

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

    //method stuff 

    return new String[][]arrayBox; 
    } 
} 

J'ai essayé googler sur la façon de retourner des tableaux de chaînes 2D, mais je ne comprends pas comment retourner.

Je suis en train de perdre la "dimension tableau".

+0

en double possible de [Java - comment revenir dans une méthode tableau multidimensionnel sans alias ing] (http://stackoverflow.com/questions/20519100/java-how-to-return-in-a-method-multidimensional-array-without-aliasing) – Oghli

Répondre

2

Il y a deux problèmes avec votre code:

(1) type de drawBox signature de la méthode de retour doit être tableau 2D-à-dire, String[][], votre signature de la méthode actuelle ne peut revenir seul tableau dimensions

(2) déclaration return devrait être comme return arrayBox (pas besoin de spécifier le type de variable à nouveau)

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