Reputation: 8103
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.
Upvotes: 0
Views: 646
Reputation: 938
for (int i = 0; i < dataEntriesg.length ; i++) {
Arrays.fill(dataEntriesg [i], "");
}
Upvotes: 0
Reputation: 6739
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
How to traverse and intilize the 2D array you can follow following sample as your blue print:
Upvotes: 1
Reputation: 347184
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
Upvotes: 2