Home>

### java - questions about double array

1. What will be displayed by the following code?

public class Test {
public static void main (String [] args) {
double [] [] m = {{1, 2, 3}, {1.5, 2.5, 3.5}, {0.1, 0.1, 0.1}}
System.out.println (sum (m));
}

public static double sum (double [] [] m) {
double sum = 0;

for (int i = 0;i<3;i ++)
sum + = m [i] [i];

return sum;
}
}

b. 3.0
d. 4.0
e. 3.6
Key: e

I don't know why this is the answer.

I guess I don't know how to view the array, so I ’ll chew and explain.

``````double [] [] m = {{1, 2, 3}, {1.5, 2.5, 3.5}, {0.1, 0.1, 0.1}}
Array view {{}, {}, {}}
m [0], m [1], m [2]
↓
{one two Three}
M [0] [0], m [0] [1], m [0] [2]
m [0], m [1], m [2]
↓
{1.5, 2.5, 3.5}
M [1] [0], m [1] [1], m [1] [2]
m [0], m [1], m [2]
↓
{1.5, 2.5, 3.5}
M [2] [0], m [2] [1], m [2] [2]
for (int i = 0;i<3;i ++)
sum + = m [i] [i];
Since i = 0 i = 1 i = 2 is looped in the for statement, assign it.
sum + = m [0] [0];sum + = 1;The sum of sum is 1.
sum + = m [1] [1];sum + = 2.5;The sum of sum is 3.5.
sum + = m [2] [2];sum + = 0.1;The sum of sum is 3.6.``````