独特的元素的数组? [英] Array of unique elements?

查看:112
本文介绍了独特的元素的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于类似下面,我想知道如果有一个简单的方法来把这个数组的数组只有唯一值的数组?

这是给定的:

 数字= {5,5,4,3,1,4,5,4,5}

把它变成一个结果数组这样,preserving原来的顺序:

  {5,1,2,3,4}


解决方案

最简单的方法是创建从阵列设置。

 整数[]数组= ...
SET<整数GT;集=新LinkedHashSet<整数GT;(Arrays.asList(阵列));

然后就可以使用检索数组:

  set.toArray()

使用 LinkedHashSet 如果您想维持秩序或 TreeSet中如果你想拥有它排序。

Given an array like the one below, I was wondering if there is an easy way to turn this array into an array with unique values only?

This is given:

   numbers={5,5,4,3,1,4,5,4,5} 

Turn it into a result array like this, preserving the original order:

   {5,1,2,3,4} 

解决方案

The simplest way would be to create set from the array.

Integer[] array = ...
Set<Integer> set = new LinkedHashSet<Integer>(Arrays.asList(array ));

and then you can retrieve the array using:

set.toArray()

use LinkedHashSet if you want to maintain the order or TreeSet if you want to have it sorted.

这篇关于独特的元素的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆