package bianchengjichu;
public class ShuZuDemo {
public static void main(String[] args) {
// 一维数组
int[] arr = new int[3];// 静态初始化,初始化数组长度为3即数组中有三个元素,初始值都为0
int[] arr2 = { 1, 2, 3, 4 };// 静态初始化,已经给过值
arr[0] = 2;// arr数组第一个数赋值为2
System.out.println("arr[0] = " + arr[0]);
System.out.println("arr[1] = " + arr[1]);
System.out.println("arr[2] = " + arr[2]);
System.out.println("数组的长度" + arr.length);
System.out.println("arr2[0] = " + arr2[0]);
System.out.println("arr2[1] = " + arr2[1]);
System.out.println("arr2[2] = " + arr2[2]);
// 多维数组
int[][] array = new int[3][];// 定义一个行数为3行,列数不确定的二维数组
array[0] = new int[] { 11, 12 };// 为数组元素赋值
array[1] = new int[] { 21, 22, 23 };
array[2] = new int[] { 31, 32, 33, 34 };
int sum = 0;
// array.length是行数
for (int i = 0; i < array.length; i++) {
// array[i].length是列数,是第i行的列数
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
}
System.out.println(sum);
}
}