首页 > 编程 > Java > 正文

java数组去重

2019-11-08 01:55:56
字体:
来源:转载
供稿:网友

1、背景

根据不同的业务逻辑,经常会遇到数组中存在多个重复元素的场合,总结了下数组的排序,留个记录。

2、实现方法

总结了四种方法,接下来进行展示

1、方法一

[java] view plain copy 在CODE上查看代码片              //数组去重方法一  String[] array = {"a","b","c","c","d","e","e","e","a"};  List<String> result = new ArrayList<>();  boolean flag;  for(int i=0;i<array.length;i++){      flag = false;      for(int j=0;j<result.size();j++){          if(array[i].equals(result.get(j))){              flag = true;              break;          }      }      if(!flag){          result.add(array[i]);      }  }  String[] arrayResult = (String[]) result.toArray(new String[result.size()]);  System.out.PRintln(Arrays.toString(arrayResult));  先遍历原数组,然后遍历结束集,通过每个数组的元素和结果集中的元素进行比对,若相同则break。若不相同,则存入结果集。两层循环进行遍历得出最终结果。2、方法二[java] view%20plain copy //数组去重方法二  String[] array = {"a","b","c","c","d","e","e","e","a"};  List<String> list = new ArrayList<>();  list.add(array[0]);  for(int i=1;i<array.length;i++){      if(list.toString().indexOf(array[i]) == -1){              list.add(array[i]);      }  }  String[] arrayResult = (String[]) list.toArray(new String[list.size()]);  System.out.println(Arrays.toString(arrayResult));  通过使用indexOf方法进行判断结果集中是否存在了数组元素。3、方法三[java] view%20plain copy //数组去重方法三  String[] array = {"a","b","c","c","d","e","e","e","a"};  List<String> list = new ArrayList<>();  for(int i=0;i<array.length;i++){      for(int j=i+1;j<array.length;j++){          if(array[i] == array[j]){              j = ++i;          }      }      list.add(array[i]);  }  String[] arrayResult = (String[]) list.toArray(new String[list.size()]);  System.out.println(Arrays.toString(arrayResult));  嵌套循环,进行比较获取满足条件结果集。4、方法四[java] view%20plain copy         //数组去重方法四          String[] array = {"a","b","c","c","d","e","e","e","a"};          Arrays.sort(array);          List<String> list = new ArrayList<>();          list.add(array[0]);          for(int i=1;i<array.length;i++){              if(!array[i].equals(list.get(list.size()-1))){                  list.add(array[i]);              }          }  <pre name="code" class="java"><span style="white-space:pre">        </span>String[] arrayResult = (String[]) list.toArray(new String[list.size()]);  System.out.println(Arrays.toString(arrayResult));先使用java提供的数组排序方法进行排序,然后进行一层for循环,进行相邻数据的比较即可获得最终结果集。5、方法五[java] view%20plain copy //数组去重方法五          String[] array = {"a","b","c","c","d","e","e","e","a"};          Set<String> set = new HashSet<>();          for(int i=0;i<array.length;i++){              set.add(array[i]);          }          String[] arrayResult = (String[]) set.toArray(new String[set.size()]);          System.out.println(Arrays.toString(arrayResult));  感谢漂泊一剑客 的提议,加入set方法进行添加,虽然是无序排列,但是也更方便的解决了去重的问题。3、知识说明1、ArrayList集合转数组[java] view%20plain copy String[] arrayResult = (String[]) list.toArray(new String[list.size()]);  对应的java方法APItoArray

public%20Object[]%20toArray()Returns%20an%20array%20containing%20all%20of%20the%20elements%20in%20this%20list%20in%20proper%20sequence%20(from%20first%20to%20last%20element).The%20returned%20array%20will%20be%20"safe"%20in%20that%20no%20references%20to%20it%20are%20maintained%20by%20this%20list.%20(In%20other%20Words,%20this%20method%20must%20allocate%20a%20new%20array).%20The%20caller%20is%20thus%20free%20to%20modify%20the%20returned%20array.

This%20method%20acts%20as%20bridge%20between%20array-based%20and%20collection-based%20APIs.

Specified%20by:toArray in%20interface Collection<E>Specified%20by:toArray in%20interface List<E>Overrides:toArray in%20class AbstractCollection<E>Returns:an%20array%20containing%20all%20of%20the%20elements%20in%20this%20list%20in%20proper%20sequenceSee%20Also:Arrays.asList(Object[])toArray

public%20<T>%20T[]%20toArray(T[] a)Returns%20an%20array%20containing%20all%20of%20the%20elements%20in%20this%20list%20in%20proper%20sequence%20(from%20first%20to%20last%20element);%20the%20runtime%20type%20of%20the%20returned%20array%20is%20that%20of%20the%20specified%20array.%20If%20the%20list%20fits%20in%20the%20specified%20array,%20it%20is%20returned%20therein.%20Otherwise,%20a%20new%20array%20is%20allocated%20with%20the%20runtime%20type%20of%20the%20specified%20array%20and%20the%20size%20of%20this%20list.If%20the%20list%20fits%20in%20the%20specified%20array%20with%20room%20to%20spare%20(i.e.,%20the%20array%20has%20more%20elements%20than%20the%20list),%20the%20element%20in%20the%20array%20immediately%20following%20the%20end%20of%20the%20collection%20is%20set%20to null.%20(This%20is%20useful%20in%20determining%20the%20length%20of%20the%20list only if%20the%20caller%20knows%20that%20the%20list%20does%20not%20contain%20any%20null%20elements.)

Specified%20by:toArray in%20interface Collection<E>Specified%20by:toArray in%20interface List<E>Overrides:toArray in%20class AbstractCollection<E>Parameters:a -%20the%20array%20into%20which%20the%20elements%20of%20the%20list%20are%20to%20be%20stored,%20if%20it%20is%20big%20enough;%20otherwise,%20a%20new%20array%20of%20the%20same%20runtime%20type%20is%20allocated%20for%20this%20purpose.Returns:an%20array%20containing%20the%20elements%20of%20the%20listThrows:ArrayStoreException -%20if%20the%20runtime%20type%20of%20the%20specified%20array%20is%20not%20a%20supertype%20of%20the%20runtime%20type%20of%20every%20element%20in%20this%20listNullPointerException -%20if%20the%20specified%20array%20is%20null2、数组直接打印到控制台直接调用Arrays的toString方法进行转换再进行打印操作。实例:[java] view%20plain copy 派生到我的代码片System.out.println(Arrays.toString(arrayResult));  

4、总结

仅仅是根据自己想法进行总结,肯定还有更多更优的方法能够去实现,希望大神指出教导。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表