二维数组的练习
public class w { public static void main(String[] args){ int[][] m=new int[][]{{3,8,2},{2,7},{9,0,1,6}}; int sum=0; for (int i=0;i<m.length;i++){ for (int j=0;j<m[i].length;j++){ System.out.print(m[i][j]+"\t"); sum+=m[i][j]; } System.out.println(); } System.out.println(sum); } }
public class w { public static void main(String[] args){ int[][] a=new int[10][]; //初始化二维数组 for (int i=0;i<a.length;i++){ a[i]=new int[i+1];//第i行有i+1个元素 } //为每个二维数组赋值 for (int i=0;i<a.length;i++){ for (int j=0;j<a[i].length;j++){ a[i][0]=a[i][i]=1; if (i>1 && j>0 && j<i){ a[i][j]=a[i-1][j-1]+a[i-1][j]; } } } //遍历二维数组 for (int i=0;i<a.length;i++){ for (int j=0;j<a[i].length;j++){ System.out.print(a[i][j]+"\t"); } System.out.println(); } } }