Java 如何计算数组中元素的发生率?

Java 如何计算数组中元素的发生率?,java,arrays,loops,for-loop,logic,Java,Arrays,Loops,For Loop,Logic,我写了一个类,如下所示 public class Countletter { public static void main(String args[]) throws IOException { String str = "muhammed"; char[] Array = str.toCharArray(); for(int i=0;i<8;i++) { int count=1;

我写了一个类,如下所示

 public class Countletter 
 {
    public static void main(String args[]) throws IOException
    {        
      String str = "muhammed"; 
      char[] Array = str.toCharArray();

     for(int i=0;i<8;i++)
     {
       int count=1;
       for(int j=i+1;j<8;j++)
       {
        if(Array[i]==(Array[j]))
        {
            count++;
        }
       }
    System.out.println(""+Array[i]+":"+count);
    }
  }
}
但是我的代码打印出来像

    output :  m:3
              u:1
              h:1
              a:1
              m:2
              m:1
              e:1
              d:1

有人知道我的错在哪里吗?如果有人知道这个逻辑,请帮助我这里的问题是你的逻辑找到已经计数的字母数。它不显示d的计数,因为您创建了一个检查i<7的循环。该字符串是一个8个字符的字符串

import java.io.IOException;

public class Countletter 
{
public static void main(String args[]) throws IOException
{        
  int flag = 0;
  String str = "muhammed"; 
  char[] Array = str.toCharArray();

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

   for (int k = 0 ; k < i ; k++)
   {
       if (Array[k] == Array[i])
           {
           flag = 1;
           break;
           }
   }
   int count=1;

   for(int j=i+1;j<8;j++)
   {
    if(Array[i]==(Array[j]))
    {
        count++;
    }
   }
   if (flag != 1)
       System.out.println(""+Array[i]+":"+count);
  }
  }
}
import java.io.IOException;
公共类计数信
{
公共静态void main(字符串args[])引发IOException
{        
int标志=0;
String str=“穆罕默德”;
char[]数组=str.toCharArray();

对于(inti=0;i基本上,您的代码从该点开始计算每个字母的频率,因为您的循环不关心字母是否已被计数

注释中链接的答案使用的是
映射
,但如果出于某种原因不想使用,还有一些其他方法。我在映射之后的第一个想法是使用字符计数数组

int counts = new int[26];                 //Only counting lowercase letters
for(int i=0; i<counts.size(); i++)
    counts[i] = 0;                        //initialize all to 0
int a = 'a';                              //get the int representation of the first lowercase letter
str = str.toLowerCase();
for(int i = 0; i<str.length; i++){
    int let = ((int)str.charAt(i))-a;     //find the appropriate index in the count
    counts[let]++;                        //increment that letters count
}
for(int i =0; i<counts.size(); i++){
    if(c > 0)
        print(char(i+a) + ": " + c);      //only print the letters that exist
}
int counts=new int[26];//只计算小写字母

对于(int i=0;i您应该使用LinkedHashMap来计数字符。LinkedHashMap保持字符出现的顺序

    String str = "muhammed"; 
    char[] array = str.toCharArray();
    Map<Character, Integer> countMap = new LinkedHashMap<Character, Integer>();
    for(char c:array) {
        Integer cnt = countMap.containsKey(c) ? countMap.get(c) + 1 : 1;
        countMap.put(c, cnt);
    }
    for(Map.Entry<Character, Integer> entry: countMap.entrySet()) {
        System.out.println("" + entry.getKey() + ": " + entry.getValue());
    }

这是一个合乎逻辑的错误

取第一个字母,检查它在字符串中再次出现的次数,然后取第二个字符,计算它再次出现的次数

同样,如果一个字符出现第二次或第三次,或者任何你应该跳过的字符,那么你会选择每个字符

下面的代码将帮助您

public class Countletter {
public static void main(String args[]) throws IOException {
    String str = "aaaabbbbabab";
    char[] Array = str.toCharArray();

    for (int i = 0; i < str.length(); i++) {
        int count = 1;
        boolean charCameAlready=check(Array,Array[i],i);
        if(charCameAlready==false){
            for (int j = i + 1; j < str.length(); j++) {
                if (Array[i] == (Array[j])) {
                    count++;
                }
            }
            System.out.println("" + Array[i] + ":" + count);
        }
    }
}

private static boolean check(char[] array, char c,int limit) {
    for(int i=0;i<limit;i++){
        if(array[i]==c){
            return true;
        }
    }
    return false;
}
公共类Countletter{
公共静态void main(字符串args[])引发IOException{
String str=“aaaabbabab”;
char[]数组=str.toCharArray();
对于(int i=0;i对于(int i=0;i而言,错误在于循环没有跳过已计数的项,例如对于m,外部循环在

i=0 and gives count 3 for positions 0,4,5
 i=4 and gives count 2 for positions 4,5
 i=5 and gives count 1 for position 5
为了防止它们再次被复制,您可以用空格或任何特殊字符替换它们,如下所示

public class Countletter 
 {
    public static void main(String args[]) throws IOException
    {        
      String str = "muhammed"; 
      char[] Array = str.toCharArray();

     for(int i=0;i<8;i++)
     {
         if(Array[i]!=' '){
          int count=1;
           for(int j=i+1;j<8;j++)
           {
            if(Array[i]==(Array[j]))
            {
             count++;
             Array[j]=' ';
            }
          }
        System.out.println(""+Array[i]+":"+count);
       }
    }
  }
}
公共类Countletter
{
公共静态void main(字符串args[])引发IOException
{        
String str=“穆罕默德”;
char[]数组=str.toCharArray();

对于(int i=0;i尽管我强烈建议您使用上面建议的Map对象,但您可以在代码中更改以下内容以使其正常工作:

String str = "muhammed";
char[] Array = str.toCharArray();

List<String> countedLetters = new ArrayList<String>();

for (int i = 0; i < 8; i++) {
    int count = 1;
    if (!countedLetters.contains("" + Array[i])) {
        for (int j = i + 1; j < 8; j++) {
            if (Array[i] == Array[j]) {
                count++;
            }
        }
        countedLetters.add("" + Array[i]);
        System.out.println("" + Array[i] + ":" + count);
    }
}
String str=“muhammed”;
char[]数组=str.toCharArray();
List countedLetters=新建ArrayList();
对于(int i=0;i<8;i++){
整数计数=1;
如果(!countedLetters.contains(“+Array[i])){
对于(int j=i+1;j<8;j++){
if(数组[i]==数组[j]){
计数++;
}
}
加上(“+Array[i]);
System.out.println(“+Array[i]+”:“+count”);
}
}

有人能给出它的示例编码吗?因为我尝试了很多方法,但我无法解决它,请帮助
public class Countletter 
 {
    public static void main(String args[]) throws IOException
    {        
      String str = "muhammed"; 
      char[] Array = str.toCharArray();

     for(int i=0;i<8;i++)
     {
         if(Array[i]!=' '){
          int count=1;
           for(int j=i+1;j<8;j++)
           {
            if(Array[i]==(Array[j]))
            {
             count++;
             Array[j]=' ';
            }
          }
        System.out.println(""+Array[i]+":"+count);
       }
    }
  }
}
String str = "muhammed";
char[] Array = str.toCharArray();

List<String> countedLetters = new ArrayList<String>();

for (int i = 0; i < 8; i++) {
    int count = 1;
    if (!countedLetters.contains("" + Array[i])) {
        for (int j = i + 1; j < 8; j++) {
            if (Array[i] == Array[j]) {
                count++;
            }
        }
        countedLetters.add("" + Array[i]);
        System.out.println("" + Array[i] + ":" + count);
    }
}