java数组去重总结
java,数组去重实现2016-08-01
<span style="white-space:pre"> </span>//数组去重方法一 String[] array = {"a","b","c","c","d","e","e","e","a"}; List<String> result = new ArrayList<>(); boolean flag; for(int i=0;i<array.length;i++){ flag = false; for(int j=0;j<result.size();j++){ if(array[i].equals(result.get(j))){ flag = true; break; } } if(!flag){ result.add(array[i]); } } String[] arrayResult = (String[]) result.toArray(new String[result.size()]); System.out.println(Arrays.toString(arrayResult));先遍历原数组,然后遍历结束集,通过每个数组的元素和结果集中的元素进行比对,若相同则break。若不相同,则存入结果集。
//数组去重方法二 String[] array = {"a","b","c","c","d","e","e","e","a"}; List<String> list = new ArrayList<>(); list.add(array[0]); for(int i=1;i<array.length;i++){ if(list.toString().indexOf(array[i]) == -1){ list.add(array[i]); } } String[] arrayResult = (String[]) list.toArray(new String[list.size()]); System.out.println(Arrays.toString(arrayResult));通过使用indexOf方法进行判断结果集中是否存在了数组元素。
//数组去重方法三 String[] array = {"a","b","c","c","d","e","e","e","a"}; List<String> list = new ArrayList<>(); for(int i=0;i<array.length;i++){ for(int j=i+1;j<array.length;j++){ if(array[i] == array[j]){ j = ++i; } } list.add(array[i]); } String[] arrayResult = (String[]) list.toArray(new String[list.size()]); System.out.println(Arrays.toString(arrayResult));嵌套循环,进行比较获取满足条件结果集。
//数组去重方法四 String[] array = {"a","b","c","c","d","e","e","e","a"}; Arrays.sort(array); List<String> list = new ArrayList<>(); list.add(array[0]); for(int i=1;i<array.length;i++){ if(!array[i].equals(list.get(list.size()-1))){ list.add(array[i]); } } <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循环,进行相邻数据的比较即可获得最终结果集。
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
toArray
public Object[] toArray()
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.
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
Arrays.asList(Object[])
toArray
public <T> T[] toArray(T[] a)
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.)
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
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.ArrayStoreException
- if the runtime type
of the specified array is not a supertype of the runtime type of every element in this listNullPointerException
- if the specified
array is nullSystem.out.println(Arrays.toString(arrayResult));