@Test
public void test04() {
String s = "ANN国国NC中国AA国CC";
char[] chars = s.toCharArray();
Arrays.sort(chars);
String s1 = new String(chars);
System.out.println("s1 = " + s1);
for (int i = 0; i < s1.length(); ) {
char c = s1.charAt(i);
int lastIndexOf = s1.lastIndexOf(c);
System.out.println(c + "->" + (lastIndexOf - i + 1));
i = lastIndexOf + 1;
}
}
获取所有字母在字符串中出现的次数 通用
本文档通过示例展示了如何使用Java对字符串中的字符数组进行排序,并演示了如何利用排序后的数组重构字符串。重点介绍了Arrays.sort()方法和charAt(), lastIndexOf()等字符串操作函数的应用。

被折叠的 条评论
为什么被折叠?



