我有字符串

a.b.c.d

我想数一下'的出现次数。,最好是一句单句俏皮话。

(之前我把这个约束表述为“不使用循环”,以防你想知道为什么每个人都试图在不使用循环的情况下回答)。


当前回答

这里是最简单和容易理解的,不使用数组,只需使用Hashmap。此外,它将计算空白,大写字符和小字符的数量,特殊字符等。

import java.util.HashMap;
  //The code by muralidharan  
    public class FindChars {
        
        public static void main(String[] args) {
            
            findchars("rererereerererererererere");
        }
        
        public static void findchars(String s){
            
            HashMap<Character,Integer> k=new HashMap<Character,Integer>();
            for(int i=0;i<s.length();i++){
                if(k.containsKey(s.charAt(i))){
                Integer v =k.get(s.charAt(i));
                k.put(s.charAt(i), v+1);
                }else{
                    k.put(s.charAt(i), 1);
                }
                
            }
            System.out.println(k);
            
        }
    
    }

O / P: {e = r = 12日13}

第二个输入:

findchars("The world is beautiful and $#$%%%%%%@@@@ is worst");

O / P: {@ = 4 = 7, = 2, b = 1, # = 1, d = 2, = 2美元,e = 2, % = 6 f = 1, h = 1, i = 3 l = 2, n = 1, o = 2, r = 2, = 3, T = 1, T = 2, u = 2, w = 2}

其他回答

这个怎么样。它没有在底层使用regexp,因此应该比其他一些解决方案更快,并且不会使用循环。

int count = line.length() - line.replace(".", "").length();

为什么不只是分割字符,然后得到结果数组的长度。数组长度总是实例数+ 1。对吧?

这里是最简单和容易理解的,不使用数组,只需使用Hashmap。此外,它将计算空白,大写字符和小字符的数量,特殊字符等。

import java.util.HashMap;
  //The code by muralidharan  
    public class FindChars {
        
        public static void main(String[] args) {
            
            findchars("rererereerererererererere");
        }
        
        public static void findchars(String s){
            
            HashMap<Character,Integer> k=new HashMap<Character,Integer>();
            for(int i=0;i<s.length();i++){
                if(k.containsKey(s.charAt(i))){
                Integer v =k.get(s.charAt(i));
                k.put(s.charAt(i), v+1);
                }else{
                    k.put(s.charAt(i), 1);
                }
                
            }
            System.out.println(k);
            
        }
    
    }

O / P: {e = r = 12日13}

第二个输入:

findchars("The world is beautiful and $#$%%%%%%@@@@ is worst");

O / P: {@ = 4 = 7, = 2, b = 1, # = 1, d = 2, = 2美元,e = 2, % = 6 f = 1, h = 1, i = 3 l = 2, n = 1, o = 2, r = 2, = 3, T = 1, T = 2, u = 2, w = 2}

在代码的某个地方,某些东西必须循环。解决这个问题的唯一方法是完全展开循环:

int numDots = 0;
if (s.charAt(0) == '.') {
    numDots++;
}

if (s.charAt(1) == '.') {
    numDots++;
}


if (s.charAt(2) == '.') {
    numDots++;
}

...等等,但你是在源代码编辑器中手动执行循环的人——而不是运行它的计算机。请看伪代码:

create a project
position = 0
while (not end of string) {
    write check for character at position "position" (see above)
}
write code to output variable "numDots"
compile program
hand in homework
do not think of the loop that your "if"s may have been optimized and compiled to
String s = "a.b.c.d";
int charCount = s.length() - s.replaceAll("\\.", "").length();

ReplaceAll(".")将替换所有字符。

PhiLho的解决方案使用ReplaceAll("[^.]",""),不需要转义,因为[. .]]表示字符“点”,而不是“任何字符”。