删除数组中的重复元素 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15756376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Duplicate Elements in an Array Java
提问by Patrick Jean
I'm trying to remove duplicate elements from a String array. For example if the input was Yellow, Yellow, Red. The output would be Yellow, Red. What do I put inside the conditional? Is there a remove method in java? Here's the method I made:
我正在尝试从 String 数组中删除重复的元素。例如,如果输入是黄色、黄色、红色。输出将是黄色,红色。我在条件中放了什么?java中有remove方法吗?这是我制作的方法:
public static String [] CompareAndDestroy(String [] array)
{
String [] newarray = new String [array.length];
for(int i = 0; i<array.length;i++)
{
for(int j = 0;j<array.length;j++)
{
if(array[i].compareTo(array[j])==0)
{
}
}
}
return array;
}
回答by NilsH
If you need it to be and return a String[]
array:
如果您需要它并返回一个String[]
数组:
Set<String> stringSet = new HashSet<>(Arrays.asList(array));
String[] filteredArray = stringSet.toArray(new String[0]);
Although I'd consider changing the type to a Set<String>
anyway since you're trying to store a list of unique elements.
尽管我会考虑将类型更改为 a Set<String>
,因为您正在尝试存储唯一元素的列表。
回答by prasanth
You can put the elements in a set as it does not allow duplicates. Then convert that set into an array. LinkedHashSet is used so that you can get the new array in the same order as your original array.
您可以将元素放在一个集合中,因为它不允许重复。然后将该集合转换为数组。使用 LinkedHashSet 以便您可以按照与原始数组相同的顺序获取新数组。
String[] arr = new String[]{"Yellow","Red","Yellow","Green"};
Set<String> set = new LinkedHashSet<String>(arr);
set.toArray(new String[0]);
回答by Ashish Thukral
You can also try this ....
你也可以试试这个......
private static String[] arrRemove(String[] strArray) {
Set<String> set = new HashSet<String>();
set.addAll((List<String>) Arrays.asList(strArray));
return (String[]) set.toArray(new String[set.size()]);
}
回答by Ritesh Kumar Gupta
Is there a remove method in java?
java中有remove方法吗?
Please note: There is no delete or remove method in JAVA, C, C++ that remove element from simple array.
请注意:JAVA、C、C++ 中没有删除或删除方法可以从简单数组中删除元素。
There are other containers like ArrayList, LinkedList,etc in JAVA from which you can delete any element(s).
JAVA 中还有其他容器,如 ArrayList、LinkedList 等,您可以从中删除任何元素。
What do I put inside the conditional to get unique elements?
我在条件中放什么来获得独特的元素?
You can either insert all the elements in a Hash-Set or you can create a new array (before first loop) and insert new elements in this new array.
您可以在 Hash-Set 中插入所有元素,也可以创建一个新数组(在第一个循环之前)并在这个新数组中插入新元素。
回答by ArgumentNullException
Store them in a hashset or linkedhashset. http://docs.oracle.com/javase/6/docs/api/java/util/Set.html
将它们存储在哈希集或链接哈希集中。 http://docs.oracle.com/javase/6/docs/api/java/util/Set.html
回答by Ankit
there is no method to remove from array. Since arrays are fixed size memory block, you can not remove or add any element. you can just assign values to elements. For your requirement, you can alternatively use, Hashset.
没有从数组中删除的方法。由于数组是固定大小的内存块,您不能删除或添加任何元素。你可以只为元素赋值。根据您的要求,您也可以使用 Hashset。
回答by rai.skumar
Convert String[] to ArrayList < String >. ArrayList has a remove method. Check API doc.
将 String[] 转换为 ArrayList <String>。ArrayList 有一个 remove 方法。检查 API 文档。
回答by Makoto
Unless this is an academic exercise, convert the elements to a Set
instead.
除非这是一个学术练习,否则将元素转换为 a Set
。
Set<String> uniqueStr = new HashSet<>(Arrays.asList(array));
String[] buf = new String[0]; // zero-length typed array for Set.toArray operation
return uniqueStr.toArray(buf); // returns all unique elements