Wednesday, May 18, 2011

Using .length instead of named constants in 2D arrays

Named constants make a program very readable, but they may not be available if the array has been passed as a parameter. You can get the size of each dimension with the .length attribute. This is the most general style.

for (int row = 0; row < a2.length; row++) {
for (int col = 0; col < a2[row].length; col++) {
output += " " + a2[row][col];
}
output += "\n";
}

No comments:

Post a Comment

Chitika