java删除相邻一样的字符_第1张图片

import java.util.*;

public class Main {
public static void main(String[] args) {
String str = "AABBCADDDACCCCCC";
System.out.println(filterCode2(str));
}

public static String filterCode2(String str) {
List list = new ArrayList();
for (int i = 0; i < str.length(); i++) {
list.add(str.charAt(i) + "");// 这里就是使用字符串存入到list中,
}
for (int m = 0; m < list.size() - 1; m++) {
if (list.get(m).equals(list.get(m + 1))) {
list.remove(m + 1);
list.remove(m);
m--;
}
}
StringBuffer sb = new StringBuffer();
String str3 = "";
for (int j = 0; j < list.size(); j++) {
str3 = sb.append(list.get(j)).toString();
}
return str3;
}
}