Hesam
Hesam

Reputation: 53600

Java, how to adjust second dimension of 2 dimensional array?

In my application I need to have a 2 dimensional array. If I define it fix it works fine, like this:

static final String arrGroupelements[] = {"India", "Australia", "England", "South Africa"};
    static final String arrChildelements[][] = { {"Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" },
                                                 {"Ponting", "Adam Gilchrist", "Michael Clarke"},
                                                 {"Andrew Strauss", "kevin Peterson", "Nasser Hussain"},
                                                 {"Graeme Smith", "AB de villiers", "Jacques Kallis"} };

However, in my code I have two lists. the first is list of recipe name that i can get it.

LinkedList<String> recipeList = dbShoppingHandler.getAllRecipeNames();
        String arrGroupelements[] = new String[recipeList.size()];
        for(int i=0; i<recipeList.size(); i++) {
            arrGroupelements[i] = recipeList.get(i);
        }

My second list is list of ingredients. In order to get list of ingredients i need to set recipe name and then i can get the list. However, i don't know how put this list as second dimension. my code is like this:

String arrChildelements[][] = new String[recipeList.size()][20];
        for(int i=0; i<recipeList.size(); i++) {
            LinkedList<String> ingredient = dbShoppingHandler.getIngredientsOfRecipeName(recipeList.get(i));
            for(int j=0; j<ingredient.size(); j++) {
                arrChildelements[i][j] = ingredient.get(j);
            }
        }

Bad thing is, i need to set a number (in my case 20) for second dimension. If i do like this for lists that have 5 items i will have 15 " " elements and those have more than 20 items the code ignore them.

First dimension is fix but i need to adjust second dimension based on number of ingredients.

any suggestion are appreciated. thanks.

Upvotes: 0

Views: 210

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533472

The simplest way to to not assume you know the length in advance.

String[][] arrChildelements[] = new String[recipeList.size()][];
for(int i=0; i<recipeList.size(); i++) {
    List<String> ingredient = dbShoppingHandler.getIngredientsOfRecipeName(recipeList.get(i));
    arrChildelements[i] = ingredient.toArray(new String[0]);
}

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328556

I suggest not to use 2D arrays for dynamic structures. Arrays are immutable, so you have to copy them, create gaps and move elements around. The standard Java library doesn't offer many useful methods to do that.

Instead, use a list of lists:

List<List<String>> data = new ArrayList<List<String>>();

Lists have many useful methods to append elements, insert and remove them and they will make you life much easier.

Upvotes: 1

MByD
MByD

Reputation: 137282

How about assigning an array in the desired sise:

String arrChildelements[][] = new String[recipeList.size()][];
//                     not mentioning second dimension size ^^
for(int i=0; i<recipeList.size(); i++) {
    LinkedList<String> ingredient = dbShoppingHandler.getIngredientsOfRecipeName(recipeList.get(i));
    arrChildelements[i] = String[ingredient.size()];
//   assigning new array here ^^
    for(int j=0; j<ingredient.size(); j++) {
        arrChildelements[i][j] = ingredient.get(j);
    }
}

Upvotes: 4

Related Questions