public class PlaceDemo {
public static void main(String[] args) {
//班级学生座位(二维数组)
place();
pace();
}
public static void place() {
//静态初始化数组-----数据类型[][] 数组名=new 数据类型[]{元素1,元素2,元素3,······}
//动态初始化数组-----数据类型[][] 数组名=new 数据类型[行数][列数]
String[][] classroom ={
//每个元素都是一维数组
{"张无忌", "赵敏", "周芷若"},// 0 第一排3个人
{"张三丰","宋远桥","林平之"},// 1 第二排3个人
{"灭绝","程昆","玄冥二老","金毛狮王"},// 2 第三排4个人
{"杨逍","纪晓芙"}// 3 第四排2个人
};
//访问数组名[行索引]
String[] names = classroom[1 ];
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
//访问数组名[行索引][列索引]
System.out.println(classroom[3][1]);
System.out.println(classroom[2][3]);
//长度访问:数组名.length
System.out.println(classroom.length);//整体的长度
System.out.println(classroom[1].length);//一维数组的长度
}
//遍历二维数组
public static void pace() {
String[][] classroom ={
//每个元素都是一维数组
{"张无忌", "赵敏", "周芷若"},// 0 第一排3个人
{"张三丰","宋远桥","林平之"},// 1 第二排3个人
{"灭绝","程昆","玄冥二老","金毛狮王"},// 2 第三排4个人
{"杨逍","纪晓芙"}// 3 第四排2个人
};
for(int i=0;i<classroom.length;i++){
//i=0, 1,2,3
String[] names=classroom[i];
for(int j=0;j<names.length;j++){
System.out.print(names[j]+" \t");
}
System.out.println();
}
// 第二种遍历方式
for(int i=0;i<classroom.length;i++){
for(int j=0;j<classroom[i].length;j++){
System.out.print(classroom[i][j]+" \t");
}
System.out.println();
}
}
}
标签:classroom,数组名,System,二维,1019,数组,println,out
From: https://blog.csdn.net/2401_86192037/article/details/143086544