打印二维数组
for
for (int[] arr : ns) {
for (int n : arr) {
System.out.print(n);
System.out.print(', ');
}
System.out.println();
}
Arrays.deepToString()
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] ns = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
System.out.println(Arrays.deepToString(ns));
}
}
计算数组平均值
public class Main {
public static void main(String[] args) {
// 用二维数组表示的学生成绩:
int[][] scores = {
{ 82, 90, 91 },
{ 68, 72, 64 },
{ 95, 91, 89 },
{ 67, 52, 60 },
{ 79, 81, 85 },
};
double sum=0;
for(int i=0;i<scores.length;i++)
{
for(int j=0;j<scores[i].length;j++)
{
sum+=scores[i][j];
}
}
double average = sum/(scores.length*scores[1].length);
System.out.println(average);
if (Math.abs(average - 77.733333) < 0.000001) {
System.out.println("测试成功");
} else {
System.out.println("测试失败");
}
}
}