public static void main(String[] args) { // TODO code application logic here String str="ABabcdefgxyzXYZ"; int len=str.length(); int a[]=new int[200]; char ch; for (int i=0;i<len;i++){ ch=str.charAt(i); a[(int)ch]++; } for (int i=65;i<=200;i++){ ch=(char)i; if (a[i]>0) System.out.println(ch+":"+a[i]); }
public static void main(String[] args) {
// TODO code application logic here
String str="ABabcdefgxyzXYZ";
int len=str.length();
int a[]=new int[200];
char ch;
for (int i=0;i<len;i++){
ch=str.charAt(i);
a[(int)ch]++;
}
for (int i=65;i<=200;i++){
ch=(char)i;
if (a[i]>0) System.out.println(ch+":"+a[i]);
}
}
}我就不讲解了,呵呵,能跑,调试过了
用charAt()得到一个字母,和字母数组比较,用indexOf()查找,如果没有找到的话会返回-1,
如果没有找到的话,把这个字母存入字母数组,对应的(相同)字母个数数组中初始化为1。
如果找到了,返回index,把对象的字母个数数组中的数字加1。大概就是这个样子:
tmpChar = args[i].charAt(j);
tmpIndex=indexOf(tChar);
if(tmpIndex!=-1)
count[tmpIndex-]+=1;
else
later[index-1]=tChar;
count[index-1]=1;
index++;
import java.util.Iterator;public class Exercise { public static void main(String[] args) {
HashMap map = getCharMount(args[0]); //打印结果
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
Object ch = it.next();
System.out.println(ch + " " + map.get(ch));
}
} /**
* 将字符串str中的字符及其出现的次数存入HashMap
*/
public static HashMap getCharMount(String str) {
HashMap map = new HashMap();
for (int i = 0; i < str.length(); i++) {
Character ch = new Character(str.charAt(i));
Integer tmp = (Integer) map.get(ch);
if (tmp == null)
map.put(ch, new Integer(1));
else
map.put(ch, new Integer((tmp.intValue() + 1)));
}
return map;
}
}