Iterate a String array inside an object

0

I built the following object:

public class TableHelper {

    private String TABLE;
    private String[] COLUMNS;

    public TableHelper (String name, String[] columns){
        this.TABLE   = name;
        this.COLUMNS = columns;
    }

    // GETTERS
    public String name(){
        return TABLE;
    }

    public String[] columns(){
        return COLUMNS;
    }

    // SETTERS
    public void name(String name){
        this.TABLE = name;
    }
    public void columns(String[] columns){
        this.COLUMNS = columns;
    }

}

And I'm popping array with a set of objectos equal to the top as follows:

public static List<TableHelper> tableList = Arrays.asList(
        new TableHelper("menu", new String[]{
                "id         PRIMARY KEY AUTOINCREMENT",
                "position   INTEGER",
                "active     INTEGER"
        }),

        new TableHelper("menu_languages", new String[]{
                "id         PRIMARY KEY AUTOINCREMENT",
                "menu_id    INTEGER",
                "language   TEXT",
                "title      TEXT",
        }),

        new TableHelper("submenu", new String[]{
                "id         PRIMARY KEY AUTOINCREMENT",
                "menu_id    INTEGER",
                "position   INTEGER",
                "active     INTEGER"
        }),

        new TableHelper("submenu_languages", new String[]{
                "id         PRIMARY KEY AUTOINCREMENT",
                "submenu_id INTEGER",
                "language   TEXT",
                "title      TEXT",
        })
);

What I need now is to iterate through the String[] COLUMNS field within each object within the array. I already have this but I know it is wrong. How can I do what I want with the loop for :

for (TableHelper tables : Config.tableList){
    for(TableHelper col : tables.columns()){
        Log.e("COLUMN", tables2.toString());
    }
}
    
asked by anonymous 13.01.2015 / 18:34

1 answer

2

Try this:

for (TableHelper tables : Config.tableList){
    for(String col : tables.columns()){
        Log.e("COLUMN", col);
    }
}

Explanation:

  • The return type of your columns method is String and not TableHelper . The compiler should have claimed this.
  • In your loop there is no tables2 variable. But even if it had, at each iteration you would be always logging the same thing, and this thing is not what you are iterating. I mean, it was not what you wanted. What you wanted is the column you're iterating, which is col .
13.01.2015 / 18:36