黑马程序员技术交流社区

标题: 张老师的数组小问题。张老师的 [打印本页]

作者: 李哲    时间: 2012-4-11 10:58
标题: 张老师的数组小问题。张老师的
下面的代码,我注释的地方为什么张老师的可以编译通过,我的不可以呢?
难道是版本问题?


  1. import java.lang.reflect.*;


  2. class ReflectTest
  3. {       
  4.         public static void main(String[] args) throws Exception
  5.         {
  6.                
  7.                
  8.                
  9.                 int[] a1=new int[3];
  10.                 int[] a2=new int[4];
  11.                 int[][] a3=new int[2][3];
  12.                 String [] a4=new String [3];
  13.                
  14.                 System.out.println(a1.getClass() == a2.getClass());       
  15.                 //System.out.println(a1.getClass() == a3.getClass());
  16.                 //System.out.println(a1.getClass() == a4.getClass());
  17.                 System.out.println(a1.getClass().getSuperclass().getName());       
  18.                
  19.                
  20.                
  21.                
  22.                
  23.         }
  24. }

  25.        
复制代码

作者: zhouxp3323    时间: 2012-4-11 11:02
我也遇到这种问题了,应该是版本的问题,现在的版本是,一个int型一维数组的class对象不能直接与int型多维数组的class对象或者一个string类型的class对象直接进行比较吧。
作者: 莫运飞    时间: 2012-4-11 11:22
程序给你改了一下,可以比较运行了

class ReflectTest
{        
        public static void main(String[] args) throws Exception
        {
               
               
               
                int[] a1=new int[3];
                int[] a2=new int[4];
                int[][] a3=new int[2][3];
                String [] a4=new String [3];
               
                System.out.println(a1.getClass() == a2.getClass());        
               System.out.println((a1.getClass()). equals (a3.getClass()));
                System.out.println((a3.getClass()). equals (a4.getClass()));
                System.out.println(a3.getClass().getSuperclass().getName());        
               
               
               
               
               
        }
}

        
作者: 王德南    时间: 2012-4-11 11:48
改为:
System.out.println((a1.getClass()). equals (a3.getClass()));
System.out.println((a3.getClass()). equals (a4.getClass()));
因为这样是比较内容是否相等的而
    System.out.println(a1.getClass() == a3.getClass());
    System.out.println(a1.getClass() == a4.getClass());
这样写的话是比较它们的地址是否相等因为这两个数在内存中的地址 是不相等的所以会编译不过去啊




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2