|NO.Z.00091|——————————|^^ 笔试 ^^|——|Java&数组单元.V10|——|Java.v10|二维数组.v02|声明使用|初始化方式|

一、[二维数组的声明和使用]——[初始化方式]——[考点]
### --- 二维数组的声明和初始化方式

~~~     ——>        数据类型[][] 数组名称= new 数据类型[行数][列数];
~~~     ——>        数据类型[][] 数组名称= {{元素1, 元素2,...}, ...};
二、编程代码
### --- 编程代码

/*
    编程实现二维数组的声明和使用
 */
public class ArrayArrayTest {
    
    public static void main(String[] args) {
        
        // 1.声明一个具有2行3列元素类型为int类型的二维数组
        int[][] arr1 = new int[2][3];
        // 打印数组中的每个元素
        // 使用外层for循环控制打印的行数
        for(int i = 0; i < arr1.length; i++) {
            // 使用内层for循环控制打印的列数
            for(int j = 0; j < arr1[i].length; j++) {
                System.out.print(arr1[i][j] + " "); // 全是0
            }
            System.out.println();
        }
        
        System.out.println("--------------------------------------------------");
        // 2.实现二维数组中元素的赋值
        int cnt = 1;
        // 使用外层for循环控制打印的行数
        for(int i = 0; i < arr1.length; i++) {
            // 使用内层for循环控制打印的列数
            for(int j = 0; j < arr1[i].length; j++) {
                arr1[i][j] = cnt++;
            }
        }
        // 使用外层for循环控制打印的行数
        for(int i = 0; i < arr1.length; i++) {
            // 使用内层for循环控制打印的列数
            for(int j = 0; j < arr1[i].length; j++) {
                System.out.print(arr1[i][j] + " "); // 1 2 3   4 5 6
            }
            System.out.println();
        }
        
        System.out.println("--------------------------------------------------");
        // 3.二维数组元素的初始化操作
        int[][] arr2 = {{11, 22, 33, 44}, {55, 66, 77, 88}};
        // 使用外层for循环控制打印的行数
        for(int i = 0; i < arr2.length; i++) {
            // 使用内层for循环控制打印的列数
            for(int j = 0; j < arr2[i].length; j++) {
                System.out.print(arr2[i][j] + " "); // 11 22 33 44   55 66 77 88
            }
            System.out.println();
        }
        
        System.out.println("--------------------------------------------------");
        // 4.考点
        int[][] arr3 = new int[3][];
        arr3[0] = new int[3];
        arr3[1] = new int[4];
        arr3[2] = new int[5];
    }
}
三、编译打印
### --- 编译

C:\Users\Administrator\Desktop\project>javac ArrayArrayTest.java
### --- 打印输出

C:\Users\Administrator\Desktop\project>java ArrayArrayTest
0 0 0
0 0 0
--------------------------------------------------
1 2 3
4 5 6
--------------------------------------------------
11 22 33 44
55 66 77 88
--------------------------------------------------

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on 2022-04-02 23:26  yanqi_vip  阅读(21)  评论(0)    收藏  举报

导航