黑马程序员技术交流社区
标题:
大神解答~~
[打印本页]
作者:
无奈的我+.l
时间:
2014-4-26 23:42
标题:
大神解答~~
public static void main(String[] args){
// TODO 自动生成的方法存根
String str ="abcdekka27qoq";
System.out.println(charCount(str));
}
public static String charCount(String str){
char[] chs = str.toCharArray();
Map<Character,Integer> m = new TreeMap<Character,Integer>();
int count =0;
for(int x=0;x<chs.length;x++){
if(chs[x]>='a'&&chs[x]<='z'||chs[x]>='A'&&chs[x]<='Z'){
Integer value = m.get(chs[x]);
if(value!=null)
count=value;
count++;
m.put(chs[x], count);
count =0;
}
}
复制代码
代码这 Integer value = m.get(chs[x]);
if(value!=null)
count=value;
count++;
m.put(chs[x], count);
count =0;
的思想 谢谢!!!
作者:
你为谁归来
时间:
2014-4-27 00:07
你这段代码要么不全,要么就有问题,你调用的方法应该有个String类型的返回值,但是没有。
如果只看你问的问题的话,我的理解如下:
Integer value = m.get(chs[x]); \\返回m集合中chs[x]键对应的值
if(value!=null) \\判断chs[x]对用的值是否为空
count=value; \\赋值动作
count++; \\ 自增动作
m.put(chs[x], count); \\ 将count这个值与chs[x]关联
count =0; \\清空,方面下一次操作。
作者:
无奈的我+.l
时间:
2014-4-27 08:10
你为谁归来 发表于 2014-4-27 00:07
你这段代码要么不全,要么就有问题,你调用的方法应该有个String类型的返回值,但是没有。
如果只看你问的 ...
谢谢,明白了!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2