lundi 20 avril 2015

How to extract a string array from a multidimensional string array and return it within a method - java?

In Java, I have String[][] and I need to return a single row (ie. String array) of this multidimensional data structure. Here is the code I am using:

private static String[] getData(String routeInfo) {
    int i = 0;
    int j = 0;
    for (j=0;j < 3; j++) {
        if (stopsByRoute[j][i].equals(routeInfo)) {
            return stopsByRoute[j][i];
        }
    }
    return null;
}

Here's the String[][] I am working with which is called stopsByRoute (Note: the array isn't complete. It's for testing):

static String[][] stopsByRoute = {
        {"1 Timberlea Express","Main St. and Franklin Ave. Transfer Stn","Eagle Ridge Gate @ Louitit Road","Powder Station"}, 
    {"2 Thickwood Express","Main St. and Franklin Ave. Transfer Stn","Signal Rd. @ Thickwood Shopping Plaza","Westwood School" }, {"3 Morgan Heights"}};

The problem is instead of returning the String[] the method returns a reference.

Aucun commentaire:

Enregistrer un commentaire