【问题标题】:printing unique char and their occurrence打印独特的字符及其出现
【发布时间】:2015-08-13 13:10:43
【问题描述】:

我是java 和编程的新手。我已经分配了一个任务来计算唯一字符的出现。我只需要使用 大批。我已经做了流动的代码 -

public class InsertChar{

    public static void main(String[] args){

        int[] charArray = new int[1000];
        char[] testCharArray = {'a', 'b', 'c', 'x', 'a', 'd', 'c', 'x', 'a', 'd', 'a'};

        for(int each : testCharArray){

            if(charArray[each]==0){
                charArray[each]=1;
            }else{
                ++charArray[each];
            }

        }

        for(int i=0; i<1000; i++){
            if(charArray[i]!=0){
                System.out.println( i +": "+ charArray[i]);
            }
        }
    }

}  

对于testCharArray,输出应该是 -

a: 4
b: 1
c: 2
d: 2
x: 2  

但它给了我以下输出 -

97: 4
98: 1
99: 2
100: 2
120: 2  

我该如何解决这个问题?

【问题讨论】:

    标签: java arrays char


    【解决方案1】:

    i 是int,因此您打印的是每个char 的整数值。您必须将其转换为 char 才能看到字符。

    改变

    System.out.println( i +": "+ charArray[i]);
    

    到

    System.out.println( (char)i +": "+ charArray[i]);
    

    【讨论】:

      【解决方案2】:

      您将索引打印为int。在打印之前尝试将其转换为char:

      for (int i=0; i<1000; i++){
          if (charArray[i]!=0){
              System.out.println( ((char) i) +": "+ charArray[i]);
          }
      }
      

      【讨论】:

        【解决方案3】:

        您正在打印每个char 的ASCII int 表示。您必须通过强制转换将它们转换为 char -

        System.out.println( (char)i +": "+ charArray[i]);
        

        【讨论】:

          【解决方案4】:

          您的程序正在打印字符的ascii 表示。您只需要将acsii 数字转换为字符。

          public class InsertChar{
          
              public static void main(String[] args){
          
                  int[] charArray = new int[1000];
                  char[] testCharArray = {'a', 'b', 'c', 'x', 'a', 'd', 'c', 'x', 'a', 'd', 'a'};
          
                  for(int each : testCharArray){
          
                      if(charArray[each]==0){
                          charArray[each]=1;
                      }else{
                          ++charArray[each];
                      }
          
                  }
          
                  for(int i=0; i<1000; i++){
                      if(charArray[i]!=0){
                          System.out.println( **(char)**i +": "+ charArray[i]);
                      }
                  }
              }
          
          }  
          

          这应该可行。

          进一步阅读:

          How to convert ASCII code (0-255) to a String of the associated character?

          【讨论】:

            【解决方案5】:

            您的 testCharArray 是一个 int 数组。您已将 char 存储在其中。所以你必须将字符转换为int。 您必须在最后一个 for 循环中进行更改 -

            for(int i=0; i<1000; i++){
                if(charArray[i]!=0){
                    System.out.println( (char)i +": "+ charArray[i]); //cast int to char.
                }
            } 
            

            这种转换使 int 变为 char。每个字符都有一个 int 值。例如 -

            'a' 有 97,因为它是 int 值
            'b' 有 98,因为它是 int 值

            【讨论】:

              猜你喜欢
              • 2017-05-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-06-11
              • 1970-01-01
              • 2017-01-02
              • 1970-01-01
              • 2017-03-01
              相关资源
              最近更新 更多