- public class Demo{
- public static void main(String[] args) {
- //可以看做一栋楼是一个数组,每层也是一个数组
- //一栋楼
- String[][] floor = new String[3][];
- //第一层
- String[] firstLayer = new String[4];
- //第二层
- String[] secondLayer = new String[3];
- //第三层
- String[] thirdLayer = new String[5];
- floor[0] = firstLayer;
- floor[1] = secondLayer;
- floor[2] = thirdLayer;
- //当天一层入住情况
- firstLayer[0] = "lucy";
- firstLayer[1] = "mike";
- firstLayer[2] = "bob ";
- firstLayer[3] = null;
- //当天二层入住情况
- secondLayer[0] = "lily";
- secondLayer[1] = null;
- secondLayer[2] = "jack";
- //当天三层入住情况
- thirdLayer[0] = null;
- thirdLayer[1] = "tom ";
- thirdLayer[2] = null;
- thirdLayer[3] = null;
- thirdLayer[4] = "rose";
- //打印当天入住情况
- for (int i = 0; i < floor.length; i++) {
- System.out.print("第" + (i + 1) + "层:");
- for (int j = 0; j < floor[i].length; j++) {
- String name = floor[i][j];
- String str = (i + 1) + "0" + (j + 1) + " " + (name == null ? "empty" : name) + "\t";
- System.out.print(str);
- }
- System.out.println();
- }
- }
- }
复制代码
输出如下:
第1层:101 lucy 102 mike 103 bob 104 empty
第2层:201 lily 202 empty 203 jack
第3层:301 empty 302 tom 303 empty 304 empty 305 rose