ID | 姓名 | 年龄 | 职能 | 入职日期 |
---|---|---|---|---|
1001 | Lucifer | 21 | PG | 2006-2-14 |
1002 | James | 22 | SF | 2007-10-10 |
1003 | Harden | 20 | SG | 2008-5-5 |
这个数组技能存储整数又能存储字符串,所以用的数据类型是Object
注意:
这里的1001不是Object对象,Java编译器会自动把基本数据类型"自动装箱"成包装类对象,后续的包装类会介绍到概念
Object[] a1 = {1001,"Lucifer",21,"PG","2006-2-14"};
Object[] a2 = {1002,"James",22,"SF","2007-10-10"};
Object[] a3 = {1003,"Harden",20,"SG","2008-5-5"};
定义一个二维数组,将上面三个数组放入:
Object[][] emps = new Object[3][];
emps[0] = a1;
emps[1] = a2;
emps[2] = a3;
实例:
package com.array;
import java.util.Arrays;
/**
* 测试数组存储表格数据
* @author Lucifer
*/
public class TestArrayTableData {
public static void main(String[] args) {
/*定义三个以为数组*/
Object[] a1 = {1001,"Lucifer",21,"PG","2006-2-14"};
Object[] a2 = {1002,"James",22,"SF","2007-10-10"};
Object[] a3 = {1003,"Harden",20,"SG","2008-5-5"};
/*定义一个二维数组*/
Object[][] tableData = new Object[3][];
tableData[0] = a1;
tableData[1] = a2;
tableData[2] = a3;
/*打印出数组的数据*/
System.out.println(Arrays.toString(tableData)); //因为二维数组,直接打印数组只能看到地址的hashcode
/*正向循环一下*/
for (Object[] temp : tableData){
System.out.println(Arrays.toString(temp));
}
}
}