【问题标题】:Displaying an ArrayList alphabetically按字母顺序显示 ArrayList
【发布时间】:2020-06-03 16:56:02
【问题描述】:

我写了一个类,它接受一个字符串,计算字符串中每个字母的出现次数,然后打印每个字母的出现次数。我希望它按字母顺序显示,但不知道该怎么做。

import java.util.ArrayList; // import the ArrayList class

class CryptCmd {

    public static void CryptCmd(String str) {

        ArrayList<String> occurs = new ArrayList<>();

        final int MAX_CHAR = 256;

        // Create an array of size 256 i.e. ASCII_SIZE
        int[] count = new int[MAX_CHAR];

        int len = str.length();

        // Initialize count array index
        for (int i = 0; i < len; i++)
            count[str.charAt(i)]++;

        // Create an array of given String size
        char[] ch = new char[str.length()];
        for (int i = 0; i < len; i++) {
            ch[i] = str.charAt(i);
            int find = 0;
            for (int j = 0; j <= i; j++) {

                // If any matches found
                if (str.charAt(i) == ch[j])
                    find++;
            }

            if (find == 1)
                occurs.add("Number of Occurrence of " + str.charAt(i) + " is: " + count[str.charAt(i)] + "\n");
        }

        System.out.println(String.join("",occurs));

        int total = 0;
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) != ' ')
                total++;
        }
        System.out.println("Total chars is " + total);
    }
}

到目前为止,打印件按找到字母的顺序显示,即

"Hello" = 
Number of Occurrence of H is: 1
Number of Occurrence of e is: 1
Number of Occurrence of l is: 2
Number of Occurrence of o is: 1

Total chars is 5

所需的输出是这个,按字母顺序排列,即

"Hello" =
Number of Occurrence of e is: 1
Number of Occurrence of H is: 1
Number of Occurrence of l is: 2
Number of Occurrence of o is: 1

Total chars is 5

【问题讨论】:

    标签: java arrays arraylist tostring


    【解决方案1】:

    它返回是因为H 在您的情况下是大写字母,在ASCII ordering 中的小写字母之前。

    A - 65 - 开始于

    a - 97 - 开始于

    这就是原因。

    如果你保持一个案例,那么它是正确的。否则,您必须编写自己的 comparator 订购类似于 this 的内容。

    【讨论】:

      【解决方案2】:

      这是代码

      
      import java.util.*; // import the ArrayList class
      
      
      class CryptCmd1 {
          public static void main(String[] args) {
              CryptCmd("Hello");
          }
          public static void CryptCmd(String str) {
      
              ArrayList<String> occurs = new ArrayList<>();
      
              final int MAX_CHAR = 256;
      
              // Create an array of size 256 i.e. ASCII_SIZE
              int[] count = new int[MAX_CHAR];
      
              int len = str.length();
      
              // Initialize count array index
              for (int i = 0; i < len; i++)
                  count[str.charAt(i)]++;
      
              // Create an array of given String size
              char[] ch = new char[str.length()];
              for (int i = 0; i < len; i++) {
                  ch[i] = str.charAt(i);
                  int find = 0;
                  for (int j = 0; j <= i; j++) {
      
                      // If any matches found
                      if (str.charAt(i) == ch[j])
                          find++;
                  }
                  String outString = "Number of Occurrence of " + str.charAt(i) + " is: " + count[str.charAt(i)] + "\n";
      
                  if (find == 1)
                      occurs.add(outString.toLowerCase());
              }
              Collections.sort(occurs);
              System.out.println(String.join("",occurs));
      
              int total = 0;
              for(int i = 0; i < str.length(); i++) {
                  if(str.charAt(i) != ' ')
                      total++;
              }
              System.out.println("Total chars is " + total);
          }
      }
      

      我已经使用collections 类对数组进行排序并满足您的要求

      【讨论】:

      • 打印现在按字母顺序工作,但出现的所有字母都返回 0
      • @ScottAdamson 很抱歉看到编辑后的代码现在可以使用混合大小写字母.....
      【解决方案3】:

      打印前只需添加以下排序逻辑

      Collections.sort(occurs, (a,b) -> a.compareToIgnoreCase(b));
      

      【讨论】:

        猜你喜欢
        • 2011-01-30
        • 2019-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-22
        相关资源
        最近更新 更多