Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串中每个字符出现的次数_Java_String_Char_Find Occurrences - Fatal编程技术网

Java 字符串中每个字符出现的次数

Java 字符串中每个字符出现的次数,java,string,char,find-occurrences,Java,String,Char,Find Occurrences,可能重复: 如何获取字符串中每个字符的出现次数?例如: “堆栈溢出”: s:1 t:1 a:1 c:1 k:1 o:2 v:1 e:1 r:1 f:1 l:1 o:1 w:1我建议使用从字符到计数的映射,并在添加到映射或计数的字符数组上进行迭代创建HashMap,其中键作为字符,整数计数作为值 HashMap<Character, Integer> hm = new HashMap<Character, Integer>() for (int i = 0; i <

可能重复:

如何获取字符串中每个字符的出现次数?例如:

“堆栈溢出”: s:1 t:1 a:1 c:1 k:1 o:2 v:1 e:1 r:1 f:1 l:1 o:1
w:1

我建议使用从字符到计数的映射,并在添加到映射或计数的字符数组上进行迭代

创建HashMap,其中键作为字符,整数计数作为值

HashMap<Character, Integer> hm = new HashMap<Character, Integer>()
for (int i = 0; i < str.length; i++) {
    if (hm.get(str.charAt(i))) {
        int temp = hm.get(str.charAt(i));
        hm.put(str.charAt(i), ++temp);
    } else {
        hm.put(str.charAt(i), 1);
    }
}
HashMap hm=newhashmap()
对于(int i=0;i
您可以解析字符串并创建一个
映射
,这样整数(映射中的值)表示字符c(映射中的键)在字符串中出现的次数。

公共静态映射计数(字符串s){
public static Map<Character, Integer> count(String s) {
  Map<Character, Integer> result = new HashMap<Character,Integer>();
  for (int i = 0; i < s.length(); ++i) {
    char c = s.charAt(i);
    Integer n = result.get(c);
    result.put(c, n == null ? 1 : n + 1);
  }

  return result;
}
映射结果=新的HashMap(); 对于(int i=0;i
Map chars=new HashMap();
对于(int i=0;i
我过去做过的一种简单但直观的方法是将字符强制转换为int,然后只使用数组。使用Map是一种更好的方法,但这听起来像是一个家庭作业问题,对于初学者来说,使用casting和数组更符合逻辑(imo)

int counts[] = new int[26];
String s = 'stackoverflow';
s = s.toLowerCase();
for (int i = 0; i < s.length(); i++) {
    int val = (int)(s.charAt(i)) - 97;
    counts[val]++;
}
int counts[]=新int[26];
字符串s='stackoverflow';
s=s.toLowerCase();
对于(int i=0;i
String input=“stackoverflow”;
//期望输出:s:1t:1a:1c:1k:1o:2v:1e:1r:1f:1l:1o:1w:1
映射计数=新哈希映射
(Math.min(input.length(),Character.MAX_值)/2);
char ch;
为了(inti=0;i来拯救我!我简直不敢相信,这是一个java一行程序

Multiset<Character> distintCharsAndCount = HashMultiset.create(
    Chars.asList("ssssssssstackoverflow".toCharArray())
);

你在谷歌上搜索了什么?;)因为这是家庭作业,而且OP没有表现出任何努力,那么给他/她指出正确的方向会比为他们做更好吗?
String input = "stackoverflow";

// desired output: s:1 t:1 a:1 c:1 k:1 o:2 v:1 e:1 r:1 f:1 l:1 o:1 w:1
Map<Character,Integer> counts = new HashMap<Character, Integer>
( Math.min(input.length(),Character.MAX_VALUE) / 2 );
char ch;
for (int i=0; i<input.length(); ++i)
{
  ch = input.charAt(i);
  if ( !counts.containsKey(ch)) { counts.put(ch,0); }
  counts.put(ch, counts.get(ch)+1 );
}

for (Map.Entry<Character,Integer> entry : counts.entrySet() )
{
  out.print( entry.getKey() );
  out.print( ":" );
  out.print( entry.getValue());
  out.print( " " );
}
Multiset<Character> distintCharsAndCount = HashMultiset.create(
    Chars.asList("ssssssssstackoverflow".toCharArray())
);
int sCount = distintCharsAndCount.count('s'); //sets it to 9