如何从一个数组独特的项目? [英] How to get unique items from an array?
本文介绍了如何从一个数组独特的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Java初学者,我发现关于这个主题的几个主题,但他们都不为我工作。
我有一个这样的数组:
I am Java beginner, I found a few topics regarding this theme, but none of them worked for me. I have an array like this:
int[] numbers = {1, 1, 2, 1, 3, 4, 5};
和我需要得到如下的输出:
and I would need to get this output:
1, 2, 3, 4, 5
从数组每个项目只有一次。
Every item from that array just once.
但如何得到它?
推荐答案
无需编写自己的算法simpliest解决方案:
The simpliest solution without writing your own algorithm:
Integer[] numbers = {1, 1, 2, 1, 3, 4, 5};
Set<Integer> uniqKeys = new TreeSet<Integer>();
uniqKeys.addAll(Arrays.asList(numbers));
System.out.println("uniqKeys: " + uniqKeys);
值的设置界面保证唯一性。 TreeSet中还排序此值。
Set interface guarantee uniqueness of values. TreeSet additionally sorts this values.
这篇关于如何从一个数组独特的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文