先使用java提供的数组排序方法进行排序,然后进行一层for循环,进行相邻数据的比较即可获得最终结果集。5、方法五
[java] view%20plain copy//数组去重方法五 String[] array = {"a","b","c","c","d","e","e","e","a"}; Set<String> set = new HashSet<>(); for(int i=0;i<array.length;i++){ set.add(array[i]); } String[] arrayResult = (String[]) set.toArray(new String[set.size()]); System.out.println(Arrays.toString(arrayResult)); 感谢漂泊一剑客 的提议,加入set方法进行添加,虽然是无序排列,但是也更方便的解决了去重的问题。
3、知识说明
1、ArrayList集合转数组
[java] view%20plain copyString[] arrayResult = (String[]) list.toArray(new String[list.size()]); 对应的java方法API
toArray
public%20Object[]%20toArray()Returns%20an%20array%20containing%20all%20of%20the%20elements%20in%20this%20list%20in%20proper%20sequence%20(from%20first%20to%20last%20element).The%20returned%20array%20will%20be%20"safe"%20in%20that%20no%20references%20to%20it%20are%20maintained%20by%20this%20list.%20(In%20other%20Words,%20this%20method%20must%20allocate%20a%20new%20array).%20The%20caller%20is%20thus%20free%20to%20modify%20the%20returned%20array.
This%20method%20acts%20as%20bridge%20between%20array-based%20and%20collection-based%20APIs.
Specified%20by:toArray
in%20interfaceCollection<E>
Specified%20by:toArray
in%20interfaceList<E>
Overrides:toArray
in%20classAbstractCollection<E>
Returns:an%20array%20containing%20all%20of%20the%20elements%20in%20this%20list%20in%20proper%20sequenceSee%20Also:Arrays.asList(Object[])
toArray
public%20<T>%20T[]%20toArray(T[] a)Returns%20an%20array%20containing%20all%20of%20the%20elements%20in%20this%20list%20in%20proper%20sequence%20(from%20first%20to%20last%20element);%20the%20runtime%20type%20of%20the%20returned%20array%20is%20that%20of%20the%20specified%20array.%20If%20the%20list%20fits%20in%20the%20specified%20array,%20it%20is%20returned%20therein.%20Otherwise,%20a%20new%20array%20is%20allocated%20with%20the%20runtime%20type%20of%20the%20specified%20array%20and%20the%20size%20of%20this%20list.If%20the%20list%20fits%20in%20the%20specified%20array%20with%20room%20to%20spare%20(i.e.,%20the%20array%20has%20more%20elements%20than%20the%20list),%20the%20element%20in%20the%20array%20immediately%20following%20the%20end%20of%20the%20collection%20is%20set%20to null.%20(This%20is%20useful%20in%20determining%20the%20length%20of%20the%20list only if%20the%20caller%20knows%20that%20the%20list%20does%20not%20contain%20any%20null%20elements.)
Specified%20by:toArray
in%20interfaceCollection<E>
Specified%20by:toArray
in%20interfaceList<E>
Overrides:toArray
in%20classAbstractCollection<E>
Parameters:a
-%20the%20array%20into%20which%20the%20elements%20of%20the%20list%20are%20to%20be%20stored,%20if%20it%20is%20big%20enough;%20otherwise,%20a%20new%20array%20of%20the%20same%20runtime%20type%20is%20allocated%20for%20this%20purpose.Returns:an%20array%20containing%20the%20elements%20of%20the%20listThrows:ArrayStoreException
-%20if%20the%20runtime%20type%20of%20the%20specified%20array%20is%20not%20a%20supertype%20of%20the%20runtime%20type%20of%20every%20element%20in%20this%20listNullPointerException
-%20if%20the%20specified%20array%20is%20null2、数组直接打印到控制台
直接调用Arrays的toString方法进行转换再进行打印操作。实例:[java] view%20plain copySystem.out.println(Arrays.toString(arrayResult));
4、总结
仅仅是根据自己想法进行总结,肯定还有更多更优的方法能够去实现,希望大神指出教导。
新闻热点
疑难解答