Recently we learned two dimensional arrays and solved task of calculating average of all elements in it. My code was like:
int a[][] = {
{1, 2, 3, 4, 5},
{6, 4, 2, 7},
{3, 6},
{2, 6, 8},
};
int sum=0, amount=0;
for (int[] row : a)
for (int val : row) {
sum += val; amount += 1;
}
return sum / (double) amount;
The thing is that I don't like the way I calculated the amount of elements in array. I tried to use size(), it not worked, tried to use Array and Arrays classes but both can retrieve neither amount of rows nor amount of elements in some row, just as a .length property.
Question: is there any way of retrieving the amount of elements from two or more dimensional matrices without using loops?