0
Object[][] dataEntriesg = {
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
        {"","","","",""},
};

I want to initialize 2d array such as this. the way I am doing it is so stupid. How can I use a loop to do it? I have tried to put it in for loop. But seems {} can be used only at declaration.

4
  • You could use a single loop, but I think you'll find a compound for-loop easier (two loops, one within the other...) Commented Jan 27, 2015 at 23:33
  • 1
    Btw why Object? Those are Strings... Commented Jan 27, 2015 at 23:35
  • @m0skit0 coz I need to pass in this in to a function requires object[][] type Commented Jan 28, 2015 at 15:18
  • @BufBills Then why are you creating String array... Commented Jan 29, 2015 at 9:53

3 Answers 3

2

You could use a compound for-loop, something like...

dataEntriesg = new Object[25][5];
for (int row = 0; row < dataEntriesg.length; row++) {
    for (int col = 0; col < dataEntriesg[row].length; col++) {
        dataEntriesg[row][col] = "";
    }
}

...for example

Sign up to request clarification or add additional context in comments.

Comments

1

Used my sample as a blue print

For example, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional arrays and each contains four elements, as shown in the figure x.length is 3, and x[0].length, x[1].length, and x[2].length are 4

enter image description here

How to traverse and intilize the 2D array you can follow following sample as your blue print:

enter image description here

Comments

0
for (int i = 0; i < dataEntriesg.length ; i++) {
    Arrays.fill(dataEntriesg [i], "");
}

Comments

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.