急,在等待!!!!

davidjian 2003-08-25 09:53:12
统计一字符串中每一个字母出现个数
如“this is a story!"
t=2
h=1
i=2
s=3
a=1
o=1
r=1
y=1
!=1
...全文
20 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
davidjian 2003-08-25
  • 打赏
  • 举报
回复
how to do the program with hashmap?
angel21 2003-08-25
  • 打赏
  • 举报
回复
补充一下,上面whyxx的Mytest类前少导入包了(初学者有时不太容易调错,其实我也只是初学者),应该在前面添加:
import java.util.*;
如有冒犯,还请见谅.
angel21 2003-08-25
  • 打赏
  • 举报
回复
下面是小弟的程序,希望ggjj可以批评指正,帮助简化一下
/***********************************************
*Explain:统计一字符串中每一个字母出现个数
*Author:angel21
*QQ: 11747144
*Date:2003/8/26
***********************************************/
import java.util.*;

//统计类caclChars
class calcChars
{
public static void calc(String str)
{
Set tmp = new HashSet();//用于比较,使结果字符唯一
final int len = str.length();
char tmpChar = ' ';//临时存储字符串中的字符
Vector arr= new Vector();//存储字符
for (int i=0; i<len; i++)
{
tmpChar = str.charAt(i);
tmp.add(new Character(tmpChar));
}
Object[] arrChs = tmp.toArray();
int tmpLen = tmp.size();
int[] sizes = new int[tmp.size()];
for(int j=0; j<tmpLen; j++)
{ sizes[j] = 0;
arr.add((Character)arrChs[j]);
}
for(int j=0; j<tmpLen;j++)
{
tmpChar = ((Character)(arr.elementAt(j))).charValue();
for (int i=0; i<len; i++)
{
if(tmpChar == str.charAt(i))
{
sizes[j]++;
}
}
}
System.out.println("CHAR NUMBER");
for(int j=0; j<tmpLen; j++)
{
/*如果不计算空格,请把下面这两行注释去掉*/
//if (arrChs[j].equals(new Character(' ')))
// continue;
System.out.print(arrChs[j]);
System.out.println(": "+sizes[j]);
System.out.println("**************");
}

}
}

//测试类test
public class test
{
public static void main(String[] args)
{
String str = "this is a story!";
calcChars.calc(str);
}
}
davidjian 2003-08-25
  • 打赏
  • 举报
回复
please give me the TIAO SHI GUO DE programe
zhong xie
cbhyk 2003-08-25
  • 打赏
  • 举报
回复
如果仅处理英文字符,且不考虑字符出现的顺序:

String s = "this is a story!";
int[] counts = new int[128];
byte[] bytes = s.getBytes();
for(int i=0; i<bytes.length; i++)
counts[0x7f & bytes[i]]++;
for(int i=0; i<counts.length; i++)
{
if(counts[i] != 0)
System.out.println(((char) i) + "=" + counts[i]);
}
whyxx 2003-08-25
  • 打赏
  • 举报
回复
最后两行写成这样吧简单一点
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
System.out.println(entry.getKey() + "'s count is " + entry.getValue());
}
whyxx 2003-08-25
  • 打赏
  • 举报
回复
public class Mytest {

public static void main(String[] args) {
String aa = "this is a pig";
Map temp = new HashMap();
for (int i = 0; i < aa.length(); i++) {
String a = aa.substring(i, i+1);
if (temp.get(a) == null) {
temp.put(a, new Integer(1));
} else {
int count = ((Integer) temp.get(a)).intValue();
temp.put(a, new Integer(count + 1));
}
}

Set dataSet = temp.entrySet();
Iterator iter = dataSet.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
wangyanqiu 2003-08-25
  • 打赏
  • 举报
回复


笨方法很有效!

有更简单的吗?
Morgan_ma 2003-08-25
  • 打赏
  • 举报
回复
存成字符数组?一个个比对?好像方法笨了点。。。请高手指教。。。
angel21 2003-08-25
  • 打赏
  • 举报
回复
Why hashmap?To kill a chicken with cow knife!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧