java 中StringBuffer 去重复值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 中StringBuffer 去重复值相关的知识,希望对你有一定的参考价值。
String x = "abc45345dfsadfdfhgfhda";StringBuffer strB = new StringBuffer(x);
ArrayList al = new ArrayList();
for (int i = 0; i < strB.length(); i++)
for (int j = 0; j < strB.length(); j++)
if (strB.charAt(i) == strB.charAt(j) && i != j)
al.add(strB.charAt(i));
strB.deleteCharAt(i);
strB.deleteCharAt(j - 1);
System.out.println("原字符串为:" + x + "\\n新字符串为:" + strB);
System.out.print("重复的字符为:");
for (int i = 0; i < al.size(); i++)
System.out.print(al.get(i) + ",");
参考技术A 最好要求说具体一点追问
java ArrayList去重复值
public static List removeDuplicateWithOrder(List list) {
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element)){
newList.add(element);
}
}
return newList;
}
------
public static List removeDuplicateWithOrder(List list) {
Set set = new HashSet(list.size());
set.addAll(list);
List newList = new ArrayList(set.size());
newList.addAll(set);
return newList;
}
----
public static List removeDuplicateWithOrder(List list) {
List newList = new ArrayList();
for (Object o : list) {
if (!newList .contains(o)) newList.add(o);
}
return newList;
}
以上是关于java 中StringBuffer 去重复值的主要内容,如果未能解决你的问题,请参考以下文章