- public class ArrayTest
- {
- public static void main(String[] args)
- {
- int[] a1 = new int[3];
- int[] a2 = new int[4];
- int[][] a3 = new int[2][3];
- String[] a4 = new String[3];
- // Class对象只有一个可以不必用equals方法
- System.out.println(a1.getClass() == a2.getClass());
- // System.out.println(a1.getClass() == a4.getClass());//报错
- // System.out.println(a1.getClass() == a3.getClass());//报错
- System.out.println(a1.getClass().getName());
- System.out.println(a2.getClass().getName());
- System.out.println(a3.getClass().getName());
- System.out.println(a1.getClass().getSuperclass().getName());
- System.out.println(a3.getClass().getSuperclass().getName());
- System.out.println("\n" +"--------------------------------");
- Object aObj1 = a1;
- Object aObj2 = a4;
- for(int i = 0; i < ((String[])aObj2).length; i++)
- {
- System.out.print(((String[])aObj2)[i] + " ");
- }//遍历
- //Object[] aObj3 = a1;//报错,原因int[]类型是object类型,
- //所以Object aObj1 = a1;正确,而int类型不是object类型
- System.out.println("\n" + int.class.getName());
- //System.out.println(int.class.getSuperclass().getName());//抛异常
- Object[] aObj4 = a4;//正确因为String是object类型的
- for(int i = 0; i < aObj4.length; i++)
- {
- System.out.print((String)aObj4[i] + " ");
- }
- //Object[][] aObj5 = a3;//报错,原因同上,int不是Object类型
- System.out.println("\n" +"--------------------------------");
- Object aObj6 = a3;
-
- for(int i = 0; i < ((int[][])aObj6).length; i++ )
- {
- System.out.println();
- for(int j = 0; j < ((int[][])aObj6)[i].length; j++)
- {
- System.out.print(((int[][])aObj6)[i][j] + " ");
- }
- }
- System.out.println("\n" +"----------------------------");
-
- Object[] aObj7 = a3;//正确,因为int[]类型的是Object类型
- //aObj7长度为2,aObj7[i]都是一个长度为3的一位数组
- System.out.println("\n" + aObj7.length);
- for(int i = 0; i < aObj7.length; i++ )
- {
- for(int j = 0; j < ((int[])aObj7[i]).length; j++)
- {
- System.out.print(((int[])aObj7[i])[j] + " ");
- }
- System.out.println();
- }
-
- }
- }
复制代码 |
|