Java中得出字符串中每个字符串出现的次数
来源:互联网 发布:win10网络凭据是什么 编辑:程序博客网 时间:2024/06/08 18:19
1、使用HaspMap的键值来存储“字符”及“字符次数”:
String str = "1122345678";HashMap<Character, Integer> strhash = new HashMap<Character, Integer>();int num = 0;int count = 0;for (int i = 0; i <= str.length() - 1; i++) {char c = str.charAt(i);int temp = 0;for (int j = 0; j <= str.length() - 1; j++) {num = str.indexOf(c, temp);if (num != -1) {count++;temp = num + 1;continue; } else {strhash.put(c, count);count = 0;break;}}} /*Iterator iter = strhash.entrySet().iterator();// 返回一个set集合while (iter.hasNext()) {Map.Entry<Character, Integer> entry = (Map.Entry<Character, Integer>) iter.next();Object key = entry.getKey();Object value = entry.getValue();System.out.println(key + "=" + value);} */Iterator iter1 = strhash.keySet().iterator();while (iter1.hasNext()) {Object key = iter1.next();Object value = strhash.get(key);System.err.println(key + "=" + value);}
2、把字符中每个字符先取出来(使用递归的方式),然后循环查询次数,效率上比第一种差,但是好理解。
public class Test { char array_char[]; List lists=new ArrayList(); public int getInstances(String all, String choice) { int total=0; array_char=all.toCharArray(); for(int i=0;i<array_char.length;i++){ if (array_char[i]==(choice.charAt(0))){ total++; } } return total; }
public static void main(String[] args) { Test test=new Test(); String str="144745741258444174584"; List array=test.result(str); for(int i=0;i<array.size();i++) { System.out.println(array.get(i)+" 出现的次数:"+test.getInstances(str, array.get(i).toString())); } } public List result(String str) { String st=""; if(str.length()>0) { lists.add(str.substring(0,1)); st=str.replaceAll(str.substring(0,1),""); result(st); } return lists; } }
- Java中得出字符串中每个字符串出现的次数
- 查找一个字符串中每个单词的出现次数--Java
- java统计字符串中每个字符出现的次数
- java统计字符串数组中每个字符串所出现的次数
- 字符串中每个字母出现了次数
- java统计字符串中每个字符出现的次数并查询出现次数最多的字符和次数
- 打印字符串数组中每个字符串出现的次数(字符串与次数对应)
- 计算一个字符串中每个字符出现的次数
- 统计某字符串中每个字符出现的次数
- 判断字符串中每个字符出现的次数
- 统计字符串中每个字母出现的次数
- 编程求解字符串中每个字母出现的次数
- 统计字符串中每个字符出现的次数
- 统计字符串中每个字符出现的次数
- 统计每个元音字母在字符串中出现的次数
- Dictionary<>练习题:统计字符串中每个字符出现的次数
- 获取字符串中每个字母出现的次数
- 统计字符串中每个字母出现的次数。
- 2012年8月编程语言排行榜-C领衔称霸Obj-C强势爬升
- 函数内部属性[arguments.callee]
- ACM进阶指南
- C++多核高级编程 - 09 并发模型之 三 流水线 Pipe
- linux、Oracle常用命令
- Java中得出字符串中每个字符串出现的次数
- 工作中的学习
- win7 64位下mini2440 USB下载驱动安装
- 空间分析之表面分析
- 希尔排序(ShellSort)
- 有梦
- 安全高性能网站搭建(三)
- 恢复PL/SQL Developer中删除确认对话框
- Edmonds_Karp 算法 (转)