1
一个Vector
A
回答
12
最简单的方法就是使用Set<Integer>
。例如,这将打印独特的元素:
Vector<Integer> c = new Vector<Integer>();
// add elements to c
Set<Integer> unique = new HashSet<Integer>();
unique.addAll(c);
for (Integer i : unique) {
System.out.println(i);
}
2
只需创建一个使用此向量设置,然后该设置将由唯一的数字组成。
1
没有做到这一点的具体方法,但这里是一个班轮几乎是功能上等同:
Integer[] u = new HashSet<Integer>(c).toArray(new Integer[0]);
(使用LinkedHashSet
或TreeSet
维护秩序和元素进行排序也是如此。)
创建int[]
是更多的工作,并且将需要一个明确的循环,值复制到int[]
。
FWIW,在大多数情况下,最好是使用ArrayList
而不是Vector
。当您需要需要Vector
提供的线程安全类型时,或者您正在开发基于J2ME的平台时,例外情况是缺少大多数Collection
类。
+0
当然,在J2ME中,不仅ArrayList不起作用,OP的问题的基于Set集的解决方案也不会起作用。 – 2012-01-02 16:23:09
相关问题
- 1. convert <vector><string> TO <vector><int> C++,Win32
- 2. C++:矢量<对<vector<int>,INT>>
- 3. <%$,<%@,<%=,<%#...这是怎么回事?
- 4. operator <<:std :: cout << i <<(i << 1);
- 5. 的Rails搞乱我的代码以“<<<<<<< HEAD”
- 6. vector <int> v(istream_iterator <int>(cin),istream_iterator <int>());
- 7. 什么意思的符号在java中“<<<”“<< =”“<<”
- 8. << <<超载
- 9. vector <pair <int,unordered_set <int> >>为对
- 10. 在C++中初始化一个向量<vector <vector <double>>>
- 11. 什么做载体<vector<T...>>和矢量<vector<T>之间的区别......>
- 12. iterate std :: vector <std :: vector <char>>?
- 13. 将std :: map <int,vector <int>>复制到std :: map <std:string,vector <int>>
- 14. 我如何重塑Mat 2D到矢量<vector<Mat>>?
- 15. 使用>>,>,> |,||,| <,<,<<,<<
- 16. 超载<<我
- 17. Overloading << C++
- 18. 如何在PHP中使用该“<<< HTML”的东西?
- 19. 返回向量<Foo>或shared_ptr <vector<Foo>>?
- 20. 矢量<Vec3b>与OpenCV中的Vector <int>的区别
- 21. <<做什么?
- 22. 制作一个std :: funtion如果我有两个功能</p> <pre><code>void foo() { std::cout << 1 << std::endl; } void bar() { std::cout << 2 << std::endl; } </code></pre> <p>指向两个函数C++
- 23. PHP操作<<<
- 24. 将std :: shared_ptr << std :: vector <double>>传递给函数
- 25. 如何迭代boost :: variant <std :: vector <int>,std :: vector <String>>?
- 26. 我应该使用boost :: ptr_vector <T>还是vector <boost :: shared_ptr <T>>?
- 27. PHP表达<<< EOB
- 28. print <<< HTML in php?
- 29. HTML标记:<<< HTML
- 30. 遍历向量<向量<对<int, int>>>
http://stackoverflow.com/questions/562894/java-detect-duplicates-in-arraylist – Vaandu 2012-01-02 08:29:28
@Vanithi - 不,这个问题是关于寻找非唯一的数字。 – 2012-01-02 08:40:10
但是同样可以使用'Set' – Vaandu 2012-01-02 08:54:49