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.
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
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 :
for (int i = 0; i < dataEntriesg.length ; i++) {
Arrays.fill(dataEntriesg [i], "");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.