[上]JAVA学习系列模块五第五章121.二维数组_存&取&遍历
视频
[vbilibili]【尚硅谷2024最新JAVA入门视频教程(上部)JAVA零基础入门教程】 https://www.bilibili.com/video/BV1YT4y1H7YM/?p=121&share_source=copy_web&vd_source=85f561e7442caa320f4a23b57edee129[/vbilibili]
笔记
获取二维数组中的元素
1.格式:
数组名[i][j]
i:代表的是一维数组在二维数组中的索引位置
j:代表的是元素在一维数组中的索引位置
public class Demo03Array {
public static void main(String[] args) {
String[][] arr = {{"张三","李四"},{"王五","赵六","田七"},{"猪八","牛九"}};
System.out.println(arr[0][0]);
System.out.println(arr[2][0]);
System.out.println(arr[1][1]);
}
}
二维数组中存储元素
1.格式:
数组名[i][j] = 值
i:代表的是一维数组在二维数组中的索引位置
j:代表的是元素在一维数组中的索引位置
public class Demo04Array {
public static void main(String[] args) {
String[][] arr = new String[2][2];
arr[0][0] = "张飞";
arr[0][1] = "李逵";
arr[1][0] = "刘备";
arr[1][1] = "宋江";
System.out.println(arr[0][0]);
System.out.println(arr[0][1]);
System.out.println(arr[1][0]);
System.out.println(arr[1][1]);
}
}
二维数组的遍历
1.先遍历二维数组,将每一个一维数组遍历出来
2.再遍历每一个一维数组,将元素获取出来
public class Demo05Array {
public static void main(String[] args) {
String[][] arr = new String[2][2];
arr[0][0] = "张飞";
arr[0][1] = "李逵";
arr[1][0] = "刘备";
arr[1][1] = "宋江";
//遍历二维数组
for (int i = 0; i < arr.length; i++) {
/*
arr[i]代表的每一个一维数组
*/
for (int j = 0; j < arr[i].length; j++) {
System.out.println(arr[i][j]);
}
}
}
}

