2

I am a bit stuck trying to implement a method that dynamically initializes a 2D array of objects.

I know to do double brace initialization with a hashmap but in this case i don't want to do that way, i want to learn how to do it manually. I know there has to be a way.

So this is what i have so far, but is not correct:

return new Object[][] {
                          {
                              buildNewItem(someValue),
                              buildNewItem(someValue),
                              buildNewItem(someValue),
                              buildNewItem(someValue),
                              buildNewItem(someValue),
                           }    
};

As you see, I am missing the assignation of the values for the first dimension which should represent the rows(0,1,2,3...).

Could you help me find out how to complete this initialization? Creating the objects before the return statement, is not an option, I want to do it on the go, all together as a single return statement.

3 Answers 3

3

Something like this:

    return new Object[][] {new Object[]{}, new Object[]{}};
Sign up to request clarification or add additional context in comments.

Comments

2

You code is correct but its just for row 0. You can add more rows using {}

static int count = 0;
public static Integer buildNewItem() {
    return count++;
}
public static void main(String[] args) {

    System.out.println(Arrays.deepToString(new Object[][]{
            {buildNewItem(), buildNewItem(), buildNewItem()},
            {buildNewItem(), buildNewItem(), buildNewItem()} <--Use {} to separate rows
                           }));

}

Output:

[[0, 1, 2], [3, 4, 5]]

Comments

0

Manually:

Object[][] obj = new Object[ROWS][COLS];
for(int i = 0 ; i < ROWS ; i++) {
    for(int j = 0 ; i < COLS; j++) {
        obj[i][j] = buildNewItem(someValue);
    }
}

5 Comments

That is not dynamic, because you specified the Rows and cols.
@sfrj yes, and I need and initialization first. anyway it is not a pain to declare an object and then initialize it.
i understand your point, but your solution does not apply to the current scenario, since your indexes are fixed. If the array is full and you want to enter more data, then you have to increase the indexes.
@sfrj an array always has fixed dimensions. this is not related on how it is initialized. if you want dynamic sized arrays, you need ArrayList or other kind of Collections.
I know that. But despite that, there is a technique called dynamic initialization of arrays. This means that yes those index will be fixed, but you don't need to specify the length of the indexes, since them will be defined depending in the number of objects passed at initialization time. The example you gave is not correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.