今天给各位分享java统计字符串中每个字符出现的次数的知识,其中也会对java统计字符串中每个字符出现的次数用进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、java中怎么统计一个字符串中每个字符的出现次数2、java怎么实现统计一个字符串中字符出现的次数?3、java怎么实现统计一个字符串中字符出现的次数
java中怎么统计一个字符串中每个字符的出现次数
操作如下:
String str =”2342asfghgyu56asdasda”;MapString,Integer maps = new HashMapString,Integer();for(int i=0;istr.length();i++){。
String key = String.valueOf((str.charAt(i)));if(!maps.containsKey(key)),maps.put(key, 1);else{int val =maps.get(key);maps.put(key, val+1);
for(Map.Entry i : maps.entrySet()){System.out.println(i.getKey()+ “==”+i.getValue());
具体方法:
package com.haotj.demo13;import java.util.Map;import java.util.Set;importjava.util.TreeMap;public class Test,public static void main(String[] args),String str。
“asdlkfjlaksdlkjfdsjlkazxcsdklfwuertiopwrljlflsdalxvclzjlksdfljklsdfuiafjdgllfdgdaslfsdjkldskfjdsl”; MapCharacter, Integer map = countChar(str);。
//遍历//1-SetCharacter set = map.keySet();for(Character key : set)System.out.println(key + “=” + map.get(key));//2-//Map中无迭代器,不能如下使用// for(Map.EntryCharacter, Integer me : map)// {// }SetMap.EntryCharacter, Integer entrys = map.entrySet();。
for(Map.EntryCharacter, Integer me : entrys)System.out.println(“===” + me.getKey() + “=” + me.getValue()/*** 统计一个字符串中,每个字符出现的次数。
* 功能详细描述* @param str:被统计的字符串,* @return 记录了每个字符及对应该字符出现的次数,* @see [类、类#方法、类#成员]*/public static MapCharacter, Integer countChar(String str)
//用于存放字符及对应次数。TreeMapCharacter, Integer tm = new TreeMapCharacter, Integer();//得到字符串中的每个字符for(int i = 0; i str.length(); //取得字符串中每个字符,Character ch = str.charAt(i);
//在tm 对象中,判断该字符是否存在,//如果存在,则取得该key 对应的value 值,将value 值加1,再存入该集合对象,//如果不存在,则将该字符及1,存入到tm 中if(!tm.containsKey(ch)),else。
java怎么实现统计一个字符串中字符出现的次数?
这是我刚根据你的需求写的,你可以参考一下!
我办法很简单,拆分存放数组里面,然后查找数组就可以了!
其实还有更简单的办法,直接用容器比较方便,目前这个办法我自己用数组写的,我个人挺满意的!
import java.util.Scanner;
public class 统计次数
{
private static Scanner sc=new Scanner(System.in);
public static void main(String[] args)
{
System.out.println(“\n\t\t==========统计字符串中字符次数==========\n”);
init();
}//初始化!
private static void init()
{
for (; ; )
{
System.out.println(“输入文字:”);
char[] arr1=fenJie(input());
System.out.println(“查找的字:”);
char x=muBiao(fenJie(input()));
int y=chaZhao(arr1,x);
System.out.println(“你要找的是\””+x+”\”,出现”+y+”次\n===============\n”);
}
}
//录入!
private static String input()
{
String s=sc.next();
return s;
}
//分解!
private static char[] fenJie(String s)
{
char[] arr=new char[s.length()];
for (int i=0;is.length() ;i++ )
{
arr[i]=(char)(s.charAt(i));
}
return arr;
}
//查找!
private static int chaZhao(char[] arr,char x)
{
int num=0,tem=-1;
for (int a=0,b=arr.length-1;a=b ;a++,b– )
{
if(a!=barr[a]==x)num++;
if(a!=barr[b]==x)num++;
if((a==b)(arr[a]==x||arr[b]==x))num++;
tem=num;
}
return tem;
}
//目标
private static char muBiao(char[] arr)
{
return arr[0];
}
}
java怎么实现统计一个字符串中字符出现的次数
可以用String的indexof(str,fromindex)方法,循环遍历加一个计数器统计次数。
public class CountTimes {
public static void main(String[] args) {
String str=”In the entire world there’s nobody like me and my best wishes is become myself”;
int times = searchstr(“my”, str); //返回2
System.out.println(times);
}
public static int searchstr(String key, String str) {
int index = 0;//每次的搜索到的下标
int count = 0;//计数器
while (( index=str.indexOf(key, index)) != -1) {
index = index + key.length();
count++;
}
return count;
}
}
java统计字符串中每个字符出现的次数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java统计字符串中每个字符出现的次数用、java统计字符串中每个字符出现的次数的信息别忘了在本站进行查找喔。