package day02;
/*
二維數(shù)組:
一維數(shù)組:可以存放一行或一列數(shù)據(jù)
二維數(shù)組:存放表格,多行多列。可以看成一維數(shù)組,只是數(shù)組中的元素是一維數(shù)組。
*/
public class Demo06 {
public static void main(String[] args) {
int[][] cs = new int[][] {{1001, 90}, {1002, 100}, {1003, 99}, {1004, 86}};
System.out.println("學(xué)號(hào):" + cs[0][0] + "分?jǐn)?shù):" + cs[0][1]);
System.out.println("學(xué)號(hào):" + cs[3][0] + "分?jǐn)?shù):" + cs[3][1]);
// 遍歷數(shù)組
for(int i = 0; i < cs.length; i++) {
for (int j = 0; j < cs.length; j++) {
System.out.print(cs[j] + " ");
}
System.out.println();
}
// foreach 遍歷
for(int[] c : cs) {
for (int i : c) {
System.out.print(i + " ");
}
System.out.println();
}
/*
練習(xí):創(chuàng)建一個(gè)二維數(shù)組,數(shù)組中存放3個(gè)人的信息,每個(gè)人包括:姓名、年齡、身高、體重
3*4
遍歷數(shù)組,每個(gè)人的信息打印一行,用\t分隔開
*/
String[][] infos = new String[][] {{"Lily", "20", "175", "60kg"},
{"Lucy", "25", "185", "70kg"},
{"Tom", "28", "165", "65kg"}};
for (String[] info : infos) {
for (String i : info) {
System.out.print(i + "\t");
}
System.out.println();
}
}
}
|