Bootstrap

Java中数组的遍历

//遍历二维数组
for(int i = 0;i < arr.length;i++){
   
	for(int j = 0; j < arr[i].length;j++){
   
        sout(arr[i][j]);
    }
    sout(); 
}

数组练习2

在这里插入图片描述

public class ArrayExer1 {
   
    public static void main(String[] args) {
   
        int arr[][] = new int[][]{
   {
   3,5,8},{
   12,9},{
   7,0,6,4}};

        int sum = 0;//记录总和
        for (int i = 0;i < arr.length;i++){
   
            for(int j = 0;j < arr[i].length;j++) {
   
                sum += arr[i][j];
            }
        }
        System.out.println("所有元素的总和为:" + sum);
    }
}

练习3

在这里插入图片描述

package array;

/**
 * @Author: jt
 * @Date: 2022/3/28 21:59
 * @Version 1.0
 */
public class YangHuiTest {
   
    public static void main(String[] args) {
   
        //1.声明并初始化二维数组
        int[][] yangHui = new int[10][];
        //2.给数组的元素赋值
        for (int i =0;i<yangHui.length;i++){
   
            yangHui[i] = new int[i + 1];

            //2.1给首末元素赋值
            yangHui[i][0] = 1;
            yangHui[i][i] = 1;
            //2.2给每行的非首末元素赋值
            //if (i > 1){
   
                for (int j = 1; j < yangHui[i].length - 1
;