package com.atguigu.java;
public class MDArray {
public static void main(String[] args) {
//1.二维数组的声明
String[][] ps;
String ps2 [][];
String[] ps3 [];
//二维数组的初始化
//静态初始化:
ps = new String[][]{{"敖媛媛","1532133322"},{"小志林","1552233223"},{"热宝","1552233223"}};
String[][] persons = {{"敖媛媛","1532133322"},{"小志林","1552233223"},{"热宝","1552233223"}};
//动态初始化
ps2 = new String[3][2];
ps3 = new String[3][]; //声明了二维数组的长度,一维数组的长度默认值null
//给二维数组的元素赋值
// ps[0]= new String[2];
// ps[1]= new String[3];
// ps[2]= new String[2];
//2.给二维数组的元素的元素赋值
//获取二维数组元素的元素的值
// String name = ps[2][0];
// System.out.println(name);
//3遍历二维数组
//方式一
for (int i = 0; i < ps.length; i++) {
//获取一维数组
String[] pss =ps[i];
//遍历一维数组
for (int j = 0; j < pss.length; j++) {
System.out.println(pss[j]);
}
}
//方式二
for (int i = 0; i < ps.length; i++) {
for (int j = 0; j < ps[i].length; j++) {
System.out.println(ps[i][j]);
}
}
}
}
java基础:二维数组的初始化和元素遍历
最新推荐文章于 2024-11-03 11:01:20 发布