Java学习笔记 --- 二维数组

一、二维数组

二维数组
1.从定义形式上看 int[][]
2.可以这样理解,原来的一维数组的每个元素是一维数组,就构成二维数组

二、内存图

三、入门练习

使用二维数组输出如下图形
            0 0 0 0 0 0
            0 0 1 0 0 0
            0 2 0 3 0 0
            0 0 0 0 0 0

public class TwoDimensionalArray05 {
	public static void main(String[] args) {
		/*
		使用二维数组输出如下图形
			0 0 0 0 0 0
			0 0 1 0 0 0
			0 2 0 3 0 0
			0 0 0 0 0 0
		什么是二维数组
		1.从定义形式上看 int[][]
		2.可以这样理解,原来的一维数组的每个元素是一维数组,就构成二维数组
		*/
		int[][] arr = {{0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0},
					   {0, 2, 0, 3, 0, 0}, {0, 0, 0, 0, 0, 0}};
		// 输出二维图形			   
		for(int i = 0; i < arr.length; i++) {//遍历二维数组的每个数组
			// 遍历二维数组的每个元素
			for(int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();//换行
		}
	}
}

四、使用方式

1.动态初始化

public class TwoDimensionalArray02 {
	public static void main(String[] args) {
		int[][] arr = new int[2][3];
		arr[1][1] = 8;
		//遍历数组
		for(int i = 0; i < arr.length; i++) {//遍历二维数组的每个数组
			// 遍历二维数组的每个元素
			for(int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();//换行
		}
	}
}

2.动态初始化

public class TwoDimensionalArray02 {
	public static void main(String[] args) {
		int[][] arr;//声明二维数组
		arr = new int[2][3];//再开空间
	
	}
}

3.动态初始化-列数不确定

public class TwoDimensionalArray03 {
	public static void main(String[] args) {
		//一共三个一维数组,每个一维数组的元素是不一样的
		int[][] arr = new int[3][];
		//遍历数组
		for(int i = 0; i < arr.length; i++) {//遍历二维数组的每个数组
			//给每个一维数组开空间,
			//如果没有给一维数组new 那么arr[i]就是null
			arr[i] = new int[i + 1];
			//遍历一维数组,并给一维数组的每个元素赋值
			for(int j = 0; j < arr[i].length; j++) {
				arr[i][j] = i + 1;
			}
		}
		// 遍历arr输出
		for(int i = 0; i < arr.length; i++) {
			for(int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();//换行
		}
	}
}

4.静态初始化

五、细节和注意事项

六、练习一

int[][] arr = {{4, 6}, {1, 4, 5, 7}, {-2}};遍历该二维数组,并得到和

public class TwoDimensionalArray05 {
	public static void main(String[] args) {
		//int[][] arr = {{4, 6}, {1, 4, 5, 7}, {-2}};
		//遍历该二维数组,并得到和
		int[][] arr = {{4, 6}, {1, 4, 5, 7}, {-2}};
		//定义一个变量计算和
		int h = 0;
		//遍历数组
		for(int i = 0; i < arr.length; i++) {
			for(int j = 0; j < arr[i].length; j++) {
				h += arr[i][j];
				System.out.print(arr[i][j] + " ");
			}
		}
		System.out.println("\n二维数组和为" + h);
	}
}

 

练习二

使用二维数组打印一个10行的杨辉三角

public class YangHui {
	public static void main(String[] args) {
		/*
		使用二维数组打印一个10行的杨辉三角
		规律
		1.第一行有1个元素,第n行有n个元素
		2.每一行的第一个元素和最后一个元素都是1
		3.从第三行开始,大于非第一个元素和最后一个元素的元素值
		arr[i][j] = arr[i-j][j] + arr[i-j][j-1];
		*/
		//定义一个二维数组
		int[][] arr = new int [10][];
		//遍历数组
		for(int i = 0; i < arr.length; i++) {
			// 给每个一维数组开空间
			arr[i] = new int[i+1];
			// 给每个一维数组赋值
			for(int j = 0; j < arr[i].length; j++) {
				if(j == 0 || j == arr[i].length - 1) {
					arr[i][j] = 1;
				}else {
					arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
				}
			}
		}
		// 输出结果
		for(int i = 0; i < arr.length; i++) {
			for(int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + "\t");
			}
			System.out.println();//换行
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值