java数组去重「建议收藏」

java数组去重「建议收藏」1、背景根据不同的业务逻辑,经常会遇到数组中存在多个重复元素的场合,总结了下数组的排序,留个记录。2、实现方法总结了四种方法,接下来进行展示1、方法一[java] viewplain copy               //数组去重方法一  String[] array 

大家好,又见面了,我是你们的朋友全栈君。

1、背景

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

2、实现方法


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

1、方法一

[java] 
view plain  
copy

 
在CODE上查看代码片
派生到我的代码片

  1.               //数组去重方法一  
  2. String[] array = {
    “a”,“b”,“c”,“c”,“d”,“e”,“e”,“e”,“a”};  
  3. List<String> result = new ArrayList<>();  
  4. boolean flag;  
  5. for(int i=0;i<array.length;i++){  
  6.     flag = false;  
  7.     for(int j=0;j<result.size();j++){  
  8.         if(array[i].equals(result.get(j))){  
  9.             flag = true;  
  10.             break;  
  11.         }  
  12.     }  
  13.     if(!flag){  
  14.         result.add(array[i]);  
  15.     }  
  16. }  
  17. String[] arrayResult = (String[]) result.toArray(new String[result.size()]);  
  18. System.out.println(Arrays.toString(arrayResult));  

先遍历原数组,然后遍历结束集,通过每个数组的元素和结果集中的元素进行比对,若相同则break。若不相同,则存入结果集。

两层循环进行遍历得出最终结果。

2、方法二

[java] 
view plain  
copy

 
在CODE上查看代码片
派生到我的代码片

  1. //数组去重方法二  
  2. String[] array = {
    “a”,“b”,“c”,“c”,“d”,“e”,“e”,“e”,“a”};  
  3. List<String> list = new ArrayList<>();  
  4. list.add(array[0]);  
  5. for(int i=1;i<array.length;i++){  
  6.     if(list.toString().indexOf(array[i]) == –1){  
  7.             list.add(array[i]);  
  8.     }  
  9. }  
  10. String[] arrayResult = (String[]) list.toArray(new String[list.size()]);  
  11. System.out.println(Arrays.toString(arrayResult));  

通过使用indexOf方法进行判断结果集中是否存在了数组元素。


3、方法三

[java] 
view plain  
copy

 
在CODE上查看代码片
派生到我的代码片

  1. //数组去重方法三  
  2. String[] array = {
    “a”,“b”,“c”,“c”,“d”,“e”,“e”,“e”,“a”};  
  3. List<String> list = new ArrayList<>();  
  4. for(int i=0;i<array.length;i++){  
  5.     for(int j=i+1;j<array.length;j++){  
  6.         if(array[i] == array[j]){  
  7.             j = ++i;  
  8.         }  
  9.     }  
  10.     list.add(array[i]);  
  11. }  
  12. String[] arrayResult = (String[]) list.toArray(new String[list.size()]);  
  13. System.out.println(Arrays.toString(arrayResult));  

嵌套循环,进行比较获取满足条件结果集。


4、方法四

[java] 
view plain  
copy

 
在CODE上查看代码片
派生到我的代码片

  1.         //数组去重方法四  
  2.         String[] array = {
    “a”,“b”,“c”,“c”,“d”,“e”,“e”,“e”,“a”};  
  3.         Arrays.sort(array);  
  4.         List<String> list = new ArrayList<>();  
  5.         list.add(array[0]);  
  6.         for(int i=1;i<array.length;i++){  
  7.             if(!array[i].equals(list.get(list.size()-1))){  
  8.                 list.add(array[i]);  
  9.             }  
  10.         }  
  11. <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 plain  
copy

 
在CODE上查看代码片
派生到我的代码片

  1. //数组去重方法五  
  2.         String[] array = {
    “a”,“b”,“c”,“c”,“d”,“e”,“e”,“e”,“a”};  
  3.         Set<String> set = new HashSet<>();  
  4.         for(int i=0;i<array.length;i++){  
  5.             set.add(array[i]);  
  6.         }  
  7.         String[] arrayResult = (String[]) set.toArray(new String[set.size()]);  
  8.         System.out.println(Arrays.toString(arrayResult));  

感谢
漂泊一剑客
 的提议,加入set方法进行添加,虽然是无序排列,但是也更方便的解决了去重的问题。

3、知识说明

1、ArrayList集合转数组

[java] 
view plain  
copy

 
在CODE上查看代码片
派生到我的代码片

  1. String[] arrayResult = (String[]) list.toArray(new String[list.size()]);  

对应的java方法API

toArray

public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).

The returned array will be “safe” in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

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

Specified by:

toArray in interface 
Collection<E>

Specified by:

toArray in interface 
List<E>

Overrides:

toArray in class 
AbstractCollection<E>
Returns:

an array containing all of the elements in this list in proper sequence

See Also:

Arrays.asList(Object[])



toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

Specified by:

toArray in interface 
Collection<E>

Specified by:

toArray in interface 
List<E>

Overrides:

toArray in class 
AbstractCollection<E>
Parameters:

a – the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Returns:

an array containing the elements of the list

Throws:

ArrayStoreException – if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
NullPointerException – if the specified array is null

2、数组直接打印到控制台


直接调用Arrays的toString方法进行转换再进行打印操作。
实例:
[java] 
view plain  
copy

 
在CODE上查看代码片
派生到我的代码片

  1. System.out.println(Arrays.toString(arrayResult));  

4、总结


仅仅是根据自己想法进行总结,肯定还有更多更优的方法能够去实现,希望大神指出教导。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/147821.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 开挂网站免费版_开心ol辅助脚本

    开挂网站免费版_开心ol辅助脚本开讲。做一个Web游戏外挂需要的准备知识:1)需要有耐心2)熟悉HTML,JavaScript,特别是FORM3)熟悉HTTP协议,特别是Cookie,URL的编码方式和POST,GET内容格式4)熟悉游戏本身,能抽象出最优的赚钱/升级的数学模型5)掌握一门语言,白菜萝卜都可以,我比较喜欢用Python和C#6)需要一些抓包的工具,比如Fiddle…

    2025年11月8日
    3
  • 快速刷微信小程序访问量和浏览量

    快速刷微信小程序访问量和浏览量1、先开发小程序,小程序需要有亮点,毕竟新颖(这样别人才更好去点击查看)。2、流量主开通的条件是独立访客(UV)不低于1000,1000人说多不多,说少也不少,因为小程序是没有链接的,是不可以进行一个流量刷取的,独立访客是需要1000个实实在在的用户,并不是访问量。3、开发好小程序之后,自己要为自己的小程序做好宣传,前提小程序需要做的完美,小程序一定要做分享功能,将小程序分享到个人、微信群、朋友圈,这样估计很容易就达到几百了。4、后续可以去各种论坛发帖,切记不要恶意刷用户量,会导致小程序被封。5

    2022年9月18日
    3
  • 阿里开源数据同步工具–DataX

    阿里开源数据同步工具–DataX下载地址:QuickStartDataX是异构数据源离线同步工具。能够将MySQLsqlServerOracleHiveHBaseFTP之间进行稳定高效的数据同步。设计思路:网状连接-》星型连接目前支持哪些数据同步?:核心架构:推荐使用python2.67不要使用python3,0使用方法和案例:1.准备一个job….

    2022年6月28日
    53
  • origin画图标注_origin图线上的特殊符号怎么弄

    origin画图标注_origin图线上的特殊符号怎么弄Origin画图标签常见语法下标:-(x)上标:+(x)斜体:\i(x)加粗:\b(x)x下标y上标:=(x,y)

    2022年9月21日
    2
  • java内存模型_简述java内存模型

    java内存模型_简述java内存模型  什么是JMM  JMM即为JAVA内存模型(javamemorymodel)。因为在不同的硬件生产商和不同的操作系统下,内存的访问逻辑有一定的差异,结果就是当你的代码在某个系统环境下运行良好,并且线程安全,但是换了个系统就出现各种问题。Java内存模型,就是为了屏蔽系统和硬件的差异,让一套代码在不同平台下能到达相同的访问结果。JMM从java5开始的JSR-133发布后,已经…

    2025年9月12日
    5
  • 使用systemctl命令启动和关闭mysql

    使用systemctl命令启动和关闭mysql以前都用service命令管理mysql,现在liunx系统升级了,又有了新的更好的方法管理系统进程,现在我们来学习如何用systemctl命令管理mysql。Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。Systemd是一个系统管理守护进程、工具和库的集合,用于取代SystemV初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。在

    2025年7月3日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号