问题
从已经有序的数组中删除重复的数据项
实现方法一
先将重复的数据置空,也就是 0,然后移动数据。
package Suanfa;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by hushangjie on 2017/11/10.
*/
public class NoDups {
public static void nodups(int[] arr){
String[] array = {"a","b","c","c","d","e","e","e","a"};
List result = new ArrayList<>();
boolean flag;
for(int i=0;ilength;i++){
flag = false;
for(int j=0;jif(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));
}
}
实现方法二
package Suanfa;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by hushangjie on 2017/11/10.
*/
public class NoDups {
public static void nodups(int[] arr){
String[] array = {"a","b","c","c","d","e","e","e","a"};
List list = new ArrayList<>();
list.add(array[0]);
for(int i=1;ilength;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));
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于