java 实现字符数组去重
2016-09-12 00:00
281 查看
1、背景
根据不同的业务逻辑,经常会遇到数组中存在多个重复元素的场合,总结了下数组的排序,留个记录。2、实现方法
总结了四种方法,接下来进行展示1、方法一
//数组去重方法一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。若不相同,则存入结果集。
两层循环进行遍历得出最终结果。
2、方法二
//数组去重方法二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方法进行判断结果集中是否存在了数组元素。
3、方法三
//数组去重方法三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));
嵌套循环,进行比较获取满足条件结果集。
4、方法四
//数组去重方法四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循环,进行相邻数据的比较即可获得最终结果集。
5、方法五
//数组去重方法五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集合转数组
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);对应的java方法API
toArray
public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
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.
Specified by:
toArrayin interface
Collection<E>
Specified by:
toArrayin interface
List<E>
Overrides:
toArrayin class
AbstractCollection<E>
Returns:
an array containing all of the elements in this list in proper sequence
See Also:
Arrays.asList(Object[])
toArray
public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
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.)
Specified by:
)]toArrayin interface
Collection<E>
Specified by:
)]toArrayin interface
List<E>
Overrides:
)]toArrayin class
AbstractCollection<E>
Parameters:
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.
Returns:
an array containing the elements of the list
Throws:
ArrayStoreException- if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
NullPointerException- if the specified array is null
2、数组直接打印到控制台
直接调用Arrays的toString方法进行转换再进行打印操作。实例:
System.out.println(Arrays.toString(arrayResult));
4、总结
仅仅是根据自己想法进行总结,肯定还有更多更优的方法能够去实现,希望大神指出教导。相关文章推荐
- Java实现字符数组全排列
- java实现字符数组转换成字符串
- java.io.BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
- Java实现字符数组全排列的方法
- java里如何实现循环打印出字符或字符数组里的内容
- JAVA实现字符串反转,借助字符数组实现
- 用JAVA编写一个算法实现对一个字符数组的所有元素的所有组合
- java正则表达式实现提取需要的字符并放入数组【ArrayList数组去重复功能】
- Java实现字符数组单词反转
- JAVA实现字符串反转,借助字符数组实现
- @V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。
- java二进制,字节数组,字符,十六进制,BCD编码转换
- java二进制,字节数组,字符,十六进制,BCD编码转换
- java二进制,字节数组,字符,十六进制,BCD编码转换
- java实现合并两个数组
- Java为其for循环实现了针对数组和集合类的新用法
- 小实例-实现对成绩总分由高到低的排序-Java中数组对象的排序
- 数组排序的Java实现(Sorting of arrays implemented with Java)
- Java 实践(Netbeans开发)——StringTokenizer实现数组分割
- Java中实现数字金额到中文大写字符的转换!! 选择自 netfalcon 的 Blog