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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 寐海流风 中级黑马   /  2014-6-12 23:08  /  1289 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 寐海流风 于 2014-6-21 15:02 编辑

        public static void main(String[] args){
                Integer[] in = {4,2,4,6,1,2,4,7,8};
                //创建泛型<Integer>数组列表,拷贝上面数组中的不重复元素
                List<Integer> list = new ArrayList<Integer>();
                for(Integer i:in){
                        if(!list.contains(i)){
                                list.add(i);
                        }
                }
                //通过toArray方法将ArrayList转为数组
                Object[] obj = list.toArray();
                //新建Integer数组接收上面的Object数组——————问题就除在这
                Integer[] inte = (Integer[])obj;
                System.out.print("{");
                for(int x=0;x<inte.length;x++){
                        if(x<inte.length-1){
                                System.out.print(inte[x]+",");
                        }else{
                                System.out.println(inte[x]+"}");
                        }
                }
        }
        //定义一个打印方法
        public static <T>void sop(T t){
                System.out.println(t);
        }

上面代码报ClassCastException;为什么Object[]数组里的元素都是Integer类型,却转不了型呢?:Q

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

6 个回复

倒序浏览
楼主这个笨蛋 obj怎么能强制转换为Integer[]呢   把里面的元素取出来  挨个转型放到给inte[]里吧
回复 使用道具 举报
public class test {          public static void main(String[] args){          Integer[] in = {4,2,4,6,1,2,4,7,8};          //创建泛型<Integer>数组列表,拷贝上面数组中的不重复元素          List<Integer> list = new ArrayList<Integer>();          for(Integer i:in){                  if(!list.contains(i)){                          list.add(i);                  }          }          //通过toArray方法将ArrayList转为数组          Object[] obj = list.toArray();          //新建Integer数组接收上面的Object数组——————问题就除在这          //[4, 2, 6, 1, 7, 8]          List<Integer> inte = new ArrayList<Integer>();          for(int i=0;i<obj.length;i++){                  inte.add((Integer)obj[i]);          }                    System.out.print("{");          for(int x=0;x<inte.size();x++){                  if(x<inte.size()-1){                          System.out.print(inte.get(x)+",");                  }else{                          System.out.println(inte.get(x)+"}");                  }          }                       }  //定义一个打印方法  public static <T>void sop(T t){          System.out.println(t);  } }
回复 使用道具 举报

public class test {
         public static void main(String[] args){
         Integer[] in = {4,2,4,6,1,2,4,7,8};
         //创建泛型<Integer>数组列表,拷贝上面数组中的不重复元素
         List<Integer> list = new ArrayList<Integer>();
         for(Integer i:in){
                 if(!list.contains(i)){
                         list.add(i);
                 }
         }
         //通过toArray方法将ArrayList转为数组
         Object[] obj = list.toArray();
         //新建Integer数组接收上面的Object数组——————问题就除在这
         //[4, 2, 6, 1, 7, 8]
         List<Integer> inte = new ArrayList<Integer>();
         for(int i=0;i<obj.length;i++){
                 inte.add((Integer)obj[i]);
         }
         
         System.out.print("{");
         for(int x=0;x<inte.size();x++){
                 if(x<inte.size()-1){
                         System.out.print(inte.get(x)+",");
                 }else{
                         System.out.println(inte.get(x)+"}");
                 }
         }


         
         }
//定义一个打印方法
public static <T>void sop(T t){
         System.out.println(t);
}
}
不好意思  上面的代码乱了 反正你能看明白  对吧
楼主  没有办法直接转的   所以一个个的转

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

回复 使用道具 举报
张益达 发表于 2014-6-13 09:51
楼主这个笨蛋 obj怎么能强制转换为Integer[]呢   把里面的元素取出来  挨个转型放到给inte[]里吧 ...

我能说我最后找到方法直接获得Integer[]数组了吗?
请看下面我的回复。
回复 使用道具 举报
本帖最后由 寐海流风 于 2014-6-13 21:09 编辑
张益达 发表于 2014-6-13 10:20
public class test {
         public static void main(String[] args){
         Integer[] in = {4,2,4,6,1,2, ...


通过我不懈的努力。我终于找到了自己满意的答案,现在将代码发到下面!还是谢谢你。不过遍历的转型我一开始就觉得这个不是最简化的方法。

public static void main(String[] args){

  Integer[] in = {4,2,4,6,1,2,4,7,8};

  List<Integer> list = new ArrayList<Integer>();
  for(Integer i:in){
   if(!list.contains(i)){
    list.add(i);
   }
  }

  Integer[] inte= list.toArray(new Integer[list.size()]);

  System.out.print("{");
  for(int x=0;x<inte.length;x++){
   if(x<inte.length-1){
    System.out.print(inte[x]+",");
   }else{
    System.out.println(inte[x]+"}");
   }
  }
}
}
回复 使用道具 举报
很给力的帖子~~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马