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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Fightin黑马 中级黑马   /  2014-8-15 21:52  /  2273 人查看  /  15 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Fightin黑马 于 2014-9-19 18:11 编辑

数组去重复,例如: 原始数组是{1,2,3,4,3,2,1,5,6,7,9},得到结果{1,2,3,4,5,6,7,9}
class  Test
{
        public static void main(String[] args)
        {
                int[]arr1={1,2,3,4,3,2,1,5,6,7,9};
                int[]arr2=new int[arr1.length];
                int count=1;
                arr2[0]=arr1[0];
                loop2:
                for(int i=0;i<arr1.length;i++){
                        for(int j=0;j<count;j++){
                                if(arr2[j]!=arr1){
                                        if(j==count-1){
                                                arr2[count]=arr1;
                                                count++;
                                        }
                                        continue ;
                                }else{
                                        continue loop2;
                                }
                        }
                }
                int arr3[]=new int[count];
                for(int i=0;i<count;i++){
                        arr3=arr2;
                }
                print(arr1);
                System.out.println();
                print(arr3);


        }
        public static void print(int arr[]){
                for(int i=0;i<arr.length;i++){
                        System.out.print(arr+"\t");
                }
        }
}

15 个回复

倒序浏览
可以利用可变数组来实现,不用建这么多的固定数组
回复 使用道具 举报
本帖最后由 790324255a 于 2014-8-16 13:06 编辑

当然有 把数组转换成list集合 再把list集合转成set 再把set集合转成list集合 搞定得到没有重复的数组
回复 使用道具 举报
790324255a 发表于 2014-8-15 21:57
当然有 把数组转换成list集合 再把list集合转成map 再把map集合转成list集合 搞定得到没有重复的数组 ...

还没学到:'(
回复 使用道具 举报

这是最方便的不用写那么多代码  没事 后期会学到集合
回复 使用道具 举报
public static void main(String[] args) {
     
    int[] arr = new int[] { 1, 1, 2, 3, 3, 4, 5, 6, 7, 8 };
    Set<Integer> set = new TreeSet<Integer>();

    for (int i : arr) {
        set.add(i);
    }

    int[] des = new int[set.size()];
    int j = 0;
    for (Integer i : set) {
        des[j++] = i;
    }
    System.out.println(Arrays.toString(des));
}
Set我觉得好似比较好的解决办法。
回复 使用道具 举报 1 0
nadax 中级黑马 2014-8-15 22:28:44
7#
我靠楼主你牛X啊, 你才注册了一个星期 22分了 求指点啊
回复 使用道具 举报
ximi 中级黑马 2014-8-16 10:04:28
8#
  1. //1. 初始化原始数组
  2.                 Integer[] originalArray = {4,2,4,6,1,2,4,7,8};
  3.                
  4.                 //2. 初始化HashSet集合,因为HashSet集合使用了Map集合的key部分,所以可以自动的去除重复的key
  5.                 Set<Integer> setArray = new HashSet<Integer>(originalArray.length);
  6.                
  7.                 //3. 将原始数组添加到HashSet集合中,这个过程就去除了重复的值了
  8.                 setArray.addAll(Arrays.asList(originalArray));
  9.                
  10.                 //4. 打印输出结果
  11.                 System.out.println(Arrays.toString(setArray.toArray(new Integer[setArray.size()])));
复制代码
回复 使用道具 举报
nadax 发表于 2014-8-15 22:28
我靠楼主你牛X啊, 你才注册了一个星期 22分了 求指点啊

在论坛找活动赚黑马币换的,文化衫那个
回复 使用道具 举报
790324255a 发表于 2014-8-15 21:57
当然有 把数组转换成list集合 再把list集合转成map 再把map集合转成list集合 搞定得到没有重复的数组 ...

不明白你转成Map干嘛?怎么转? Map是双列集合,数据唯一的单列集合是Set吧!楼主这里还需要排序,可以选择使用TreeSet集合来实现。
回复 使用道具 举报
yuli2039 发表于 2014-8-16 12:22
不明白你转成Map干嘛?怎么转? Map是双列集合,数据唯一的单列集合是Set吧!楼主这里还需要排序,可以选 ...

是set集合 打错了
回复 使用道具 举报
算法就是每插入一个与前面每个元素比较,或者直接把HashCode与HashSet配合使用
回复 使用道具 举报
set集合  等学到集合就可以了
回复 使用道具 举报
jiali 中级黑马 2014-8-16 23:06:26
14#
不懂哦!!
回复 使用道具 举报
你们技术分都好高啊
回复 使用道具 举报
好东西,谢谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马