A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 齐连涛 中级黑马   /  2012-11-3 15:11  /  1454 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

后两行为什么报错?老师的很正常的
        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()==a4.getClass());
                //System.out.println(a1.getClass()==a3.getClass());

评分

参与人数 1技术分 +1 收起 理由
杨志 + 1

查看全部评分

4 个回复

倒序浏览
你的a3[]是二维数组不能和a1的一维数组比较
你的a4是字符串也是不能比的
你要想必的话只能把a4转成数组

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

回复 使用道具 举报
                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(a4.getClass()));
        System.out.println(a1.getClass().equals(a3.getClass()));
        System.out.println(new String("123") == new String("234"));//这个是两个同样的对象。
        //用来错误的原因:我认为两个不同类型的对象比较的话,不能用 == 比较,要用equery  比较。

{:soso_e144:}{:soso_e113:}

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 请用插入代码

查看全部评分

回复 使用道具 举报
a1.getClass()的类型是:Class<? extends int[]>
a2.getClass()的同样是:Class<? extends int[]>
a3.getClass()的类型是:Class<? extends int[][]>
a4.getClass()的类型是:Class<? extends String[]>

视频上没出问题,可能是当时的版本比较低

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

回复 使用道具 举报
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];
    System.out.println(a1.getClass().toString());//打印结果class [I
    System.out.println(a2.getClass().toString());//打印结果class [I
    System.out.println(a3.getClass().toString());//打印结果class [[I
    System.out.println(a4.getClass().toString());//打印结果class [Ljava.lang.String;
   // System.out.println(a1.getClass()==a2.getClass());
   // System.out.println(a1.getClass()==a4.getClass());
    //System.out.println(a1.getClass()==a3.getClass());
}        根据打印结果,看出a1==a2,其他的就不一样,现在你知道了吧。

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马