The Way to Programming
The Way to Programming
Could somebody tell me how to calculate the first column in the first dimension . For example, the first column consist: 10, 20, 30 and 40. The answer is 100.
public class SimpleArray {
public static void main(String[] args)
{
int[][][] resultArray = {
{{ 10, 11, 12, 13 },
{ 20, 21, 22, 23 },
{ 30, 31, 32, 33 },
{ 40, 41, 42, 43 }},
{{ 50, 51, 52, 53 },
{ 60, 61, 62, 63 },
{ 70, 71, 72, 73 },
{ 80, 81, 82, 83 }},
{{ 90, 91, 92, 93 },
{ 100, 101, 102, 103 },
{ 110, 111, 112, 113 },
{ 120, 121, 122, 123 }},};
int sum = 0;
for (int i = 0; i< resultArray.length; i++)
{
for (int j = 0; j < resultArray[i].length; j++)
{
for (int k = 0; k < resultArray[i][j].length; k++)
{
sum = sum + resultArray [i][j][k];
}
}
}
System.out.println (sum);
}
}
Changed it a bit to make it easier to understand (for me at least).
public static void main(String[] args) {
int[][][] resultArray = {
{{10, 11, 12, 13},
{20, 21, 22, 23},
{30, 31, 32, 33},
{40, 41, 42, 43}},
{{50, 51, 52, 53},
{60, 61, 62, 63},
{70, 71, 72, 73},
{80, 81, 82, 83}},
{{90, 91, 92, 93},
{100, 101, 102, 103},
{110, 111, 112, 113},
{120, 121, 122, 123}},};
int sum = 0;
// for (int i = 0; i < 1; i++) { // You only want the first dimension
// for (int j = 0; j < resultArray[i].length; j++) {
// for (int k = 0; k < 1; k++) { // You only want the first column
// System.out.printf("+ resultArray[%s][%s][%s]\n", i, j, k);
// sum += resultArray[i][j][k];
// }
// }
// }
//
for (int index=0; index
Sign in to your account