计算字符串中的字符出现次数(频率) [英] Counting character occurrences in a String (frequency)

查看:146
本文介绍了计算字符串中的字符出现次数(频率)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个GUI来计算字符串中第一个字母的出现次数。我希望它以列格式计算所有字母,例如:

I have this GUI that counts the occurances of the first letter in a string. I would like it to count all letters in column format like:

以下是我目前的情况:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JPanel;  
import javax.swing.JTabbedPane;  
import javax.swing.JTextField;  
public class Index2 extends JFrame implements ActionListener  
{  
    private JTabbedPane jtabbedPane;  
    private JPanel characterFinder;  
    JTextField enterText, countText;  

    public Index2()  
    {  
        setSize(400, 250);  
        setVisible(true);  
        setSize(400, 250);  
        setVisible(true);  
        setTitle("Total Characters");  
        setSize(300, 200);  
        JPanel topPanel = new JPanel();  
        topPanel.setLayout(new BorderLayout());  
        getContentPane().add(topPanel);  
        createCharacterFinder();  
        jtabbedPane = new JTabbedPane();  
        jtabbedPane.addTab("Count Characters", characterFinder); 
        topPanel.add(jtabbedPane, BorderLayout.CENTER);  
    }
    public void createCharacterFinder()  
    {  
        characterFinder = new JPanel();  
        characterFinder.setLayout(null); 
        JLabel enterLabel = new JLabel(  
                "Enter Some Text");  
        enterLabel.setBounds(90, 5, 260, 20);  
        characterFinder.add(enterLabel); 
        enterText = new JTextField();  
        enterText.setBounds(10, 30, 270, 70);  
        characterFinder.add(enterText);  

        JButton search = new JButton("Count Occurences of Each Letter");  
        search.setBounds(15, 100, 260, 20);  
        search.addActionListener(this);  
        characterFinder.add(search);

        countText = new JTextField();  
        countText.setBounds(80, 130, 120, 500);  
        characterFinder.add(countText);
    }
        public void actionPerformed(ActionEvent e){
            String st=enterText.getText();
            char searchedChar=enterText.getText().charAt(0);
            count(searchedChar,st);
    }
    public int count(char c, String str) {
        if (str == null) return 0;
        int cnt = 0;
        for (int i = 0;; cnt++) {
        if ((i = str.indexOf(c,i)+1) == 0) break;
        }
        countText.setText("Character "+c+" occurs "+cnt+" times");
        return cnt;
        }

    public static void main(String[] args)  
    {  
        JFrame frame = new Index2();  
        frame.setSize(300, 700);  
        frame.setVisible(true);  
    }  
}


推荐答案

A计算字符的好方法(假设ASCII字符)是利用'a'可以直接映射到数字的事实。

A nice way to count characters (Assuming ASCII characters) is to take advantage of the fact that 'a' can directly map to a number.

int[] charCounts(String s) {
  int[] counts = new int[256]; // maximum value of an ASCII character
  char[] c = s.toCharArray();
  for (int i=0;i<c.length;++i) {
      counts[c[i]]++;
  }
  return counts;
}

现在找到你可以做的任何特定元素的数量计算['a']

Now to find the count of any particular element you could do counts['a'].

这可能会使代码更整洁,只需使用上述方法进行计数然后只显示计数大致如下:

It might make the code tidier to just do the counts up front with a method like above and then just display the counts with a very roughly like:

int[] counts = charCounts("my string");
StringBuilder sb = new StringBuilder();
for (char a = 'a'; a <= 'z'; a++) {
    sb.append(a).append(" occurred ").append(counts[a]).append(" times\n");
}

这篇关于计算字符串中的字符出现次数(频率)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆