一个Java中字符串的问题 请教各路大神,这种方法是可以求出一个字符串中出现次数最多的字符个次数,但是如果有两个或多个不同字符出现的次数是一样且比其他字符多的时候,怎么把这些字符输出呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) { System.out.println(doCount("aadsaccdc")); } public static List<Character> doCount(String string){ List<Character> chars = new ArrayList<Character>(); char[] cs = string.toCharArray(); int max = 0; List<Character> find = new ArrayList<Character>(); for(char c : cs){ int count = 0; boolean isFind = false; for(Character fc : find){ if(c == fc.charValue()){ isFind = true; break; } } if(!isFind){ find.add(c); Matcher m = Pattern.compile(String.valueOf(c)).matcher(string); while(m.find()){ count++; } if(count == max){ chars.add(c); }else if(count > max){ max = count; chars.clear(); chars.add(c); } } } return chars; } 把max和cm放到 for里面。可以存到map中map.put(cm,max);遍历map比较max的值。return最大的那个 关于对象数组浅克隆的问题,求解答 集合问题 基于B/S的实时曲线 [连续两天发问无结果 今天继续求]谢谢各位 [散分]Eclipse 常用技巧 java文本清空 我要用java 的相关技术做心跳检测的客户端,和用户的上下线请请求,知道原理的请进 初学java,不知道这个代码有什么错误 一个Dialog问题 关于文件打开 请教关于抽象类的两个问题 volatile 指令重排序问题
public static void main(String[] args) {
System.out.println(doCount("aadsaccdc"));
}
public static List<Character> doCount(String string){
List<Character> chars = new ArrayList<Character>();
char[] cs = string.toCharArray();
int max = 0;
List<Character> find = new ArrayList<Character>();
for(char c : cs){
int count = 0;
boolean isFind = false;
for(Character fc : find){
if(c == fc.charValue()){
isFind = true;
break;
}
}
if(!isFind){
find.add(c);
Matcher m = Pattern.compile(String.valueOf(c)).matcher(string);
while(m.find()){
count++;
}
if(count == max){
chars.add(c);
}else if(count > max){
max = count;
chars.clear();
chars.add(c);
}
}
}
return chars;
}
可以存到map中map.put(cm,max);
遍历map比较max的值。
return最大的那个