JDK 1.4 String name = "11|12|13|14|15|12|16|14|17"; String[] array = name.split("\\|"); Set hashSet = new HashSet(); StringBuffer buff = new StringBuffer(); for (int i = 0; i < array.length; i++) { if (hashSet.add(array[i])) { // Set的add方法在该元素已经存在时返回false buff.append(array[i]).append("|"); } }
String result = buff.deleteCharAt(buff.length() - 1).toString(); System.out.println(result);
String[] array = name.split("\\|");
Set<String> hashSet = new LinkedHashSet<String>();
Collections.addAll(hashSet, array);
StringBuilder buff = new StringBuilder();
for (String string : hashSet) {
buff.append(string).append("|");
}
String result = buff.deleteCharAt(buff.length() - 1).toString();
System.out.println(result);
String name = "11|12|13|14|15|12|16|14|17";
String arr[]=name.split("|");
int course[]=new int[100];
String target="";
int a;
for(int i=0;i<arr.length;i++){
a=Integer.parseInt(arr[i]);
if(course[a]==0){
target+=arr[i];
target+="|";
course[a]=1;
}
}
return target;
}
String name = "11|12|13|14|15|12|16|14|17";
String[] array = name.split("\\|");
Set hashSet = new HashSet();
StringBuffer buff = new StringBuffer();
for (int i = 0; i < array.length; i++) {
if (hashSet.add(array[i])) { // Set的add方法在该元素已经存在时返回false
buff.append(array[i]).append("|");
}
}
String result = buff.deleteCharAt(buff.length() - 1).toString();
System.out.println(result);