admin 管理员组

文章数量: 1184232


2024年3月30日发(作者:arrow英文名)

Java统计字符串中每个字符出现次数

package ;

import p;

import or;

import ;

import ;

/**

*

* @author perry_zhao

* 统计一个字符串中每个字符出现的次数(不忽略大小写)

*/

public class CountStr {

public Map count(String str){

n("需要统计的字符串:"+str+"准备开始统计每个字符出现的

次数...");

Map map = new HashMap();

String temp ="";

for(int i=0; i<(); i++){

temp = ing(i, i+1);

if(()==0){

//map集合中没数据,将第一个数据加进去

(temp, 1);

}else{

if((temp)==null){

//map集合中没有,将其加入

(temp, 1);

}else{

//map集合中存在,将其value值加1

int n = (temp);

n+=1;

(temp, n);

}

}

}

return map;

}

public static void main(String[] args) {

Count count = new Count();

Map map = tr("this is test data");

//以下是对Map集合中的数据进行跌倒传统的方法

Set set = ();

Iterator it = or();

while(t()){

String key = (String)();

int number = (Integer)(key);

(key+":"+number+"次;");

}

}

}


本文标签: 数据 字符 集合