public static String[] distinct(String[] arr){
List<String> strings = new ArrayList<>();
for (String s: arr){
if (s != null) {
boolean isReapt = false;
for (String ss : strings) {
if (ss != null) {
if (s.equals(ss)) {
isReapt = true;
}
}
}
if (!isReapt){
strings.add(s);
}
}
}
return strings.toArray(new String[strings.size()]);
}
简单说一下原理:设置一个标记,如果待加入的数组内已经存在了待加入的字符串,那么标记isReapt为true,意为“有重复”,否则就将待加入的字符串加入待加入的数组内