统计频数问题 统计任意给定的字符串中汉字和单词的使用频率例:中国china i love中国china输出结果:中 2 国 2 china 2 i 1 love 1 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个不难啊,你用map去实现啊。把字符作为key,出现的次数作为value就好了。 那你这种输出也没有道理啊?你为什么中国这一个词要分着输出,而china连在一起输出?呵呵。 import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;public class MyTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String s = "中国china i love中国china"; Pattern p = Pattern.compile("[a-zA-Z]+"); Matcher m = p.matcher(s); ArrayList<String> words = new ArrayList<String>(); int[] count = new int[s.length()]; String word; int index; while(m.find()) { word = m.group(); index = words.indexOf(word); if(index==-1) { words.add(word); count[words.size()-1] = 1; }else { count[index]++; } } char c; for(int i=0; i<s.length(); i++) { c = s.charAt(i); if(c>255) { index = words.indexOf(c+""); if(index==-1) { words.add(c+""); count[words.size()-1] = 1; }else { count[index]++; } } } for(int i=0; i<words.size(); i++) { System.out.print(words.get(i)+": "); System.out.println(count[i]); } }} import java.util.ArrayList;public class MyTest { public static void main(String[] args) { // TODO Auto-generated method stub String s = "中国china i love中国china"; ArrayList<String> words = new ArrayList<String>(); int[] count = new int[s.length()]; char c; StringBuilder sb = new StringBuilder(); boolean flag = false; for(int i=0; i<s.length(); i++) { c = s.charAt(i); if(c>255) { rememberNode(c+"", words, count); } if(c>='a'&&c<='z' || c>='A'&&c<='Z') { flag = true; sb.append(c); }else if(flag == true) { flag = false; rememberNode(sb.toString(), words, count); sb.delete(0, sb.length()); } } if(flag == true) rememberNode(sb.toString(), words, count); for(int i=0; i<words.size(); i++) { System.out.print(words.get(i)+": "); System.out.println(count[i]); } } public static void rememberNode(String node, ArrayList<String>l, int[] count) { int index; index = l.indexOf(node); if(index==-1) { l.add(node); count[l.size()-1] = 1; }else { count[index]++; } }} // mypackage1/Words.java// 统计字符串中单词出现的次数package mypackage1;import java.util.regex.*;import java.util.*;public class Words { public static void main(String[] args) { String words = "中国china i love中国china"; Map<String, Integer> map = new HashMap<String, Integer>(); Pattern p = Pattern.compile("([a-zA-Z]+|[\u4e00-\u9fa5]+)"); Matcher m = p.matcher(words); while (m.find()) { Integer i = map.get(m.group()); map.put(m.group(), i == null ? 1 : i + 1); } System.out.println(map); }} ///:~//output:{love=1, i=1, china=2, 中国=2}// 用正则表达式分开中文和英文单词([a-zA-Z]+|[\u4e00-\u9fa5]+)。这样就可以了 发错。重新发下// mypackage1/Words.java// 统计字符串中单词出现的次数package mypackage1;import java.util.regex.*;import java.util.*;public class Words { public static void main(String[] args) { String words = "中国china i love中国china"; Map<String, Integer> map = new HashMap<String, Integer>(); Pattern p = Pattern.compile("([a-zA-Z]+|[\u4e00-\u9fa5])"); Matcher m = p.matcher(words); while (m.find()) { Integer i = map.get(m.group()); map.put(m.group(), i == null ? 1 : i + 1); } System.out.println(map); }} ///:~//output://{love=1, 中=2, 国=2, i=1, china=2} 菜鸟求解! JAVA中的package的正确与否的检查 javamail用来收邮件的INBOX的路径是什么? struts中传参数问题? 求一个简单的能同时实现类,方法,实例,属性的例子 为什么我的java编译器javac.exe打不开? 有没有人把一个一维的ArrayList转化成通用的Object,其ArrayList的每一个element 对应Object的一个属性 java中如何调用com组件 用JAVA能开发出什么大系统啊? Apache配置问题!在线等待... 数据结构顺序表问题~~ 帮忙解释下这个正则
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class MyTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "中国china i love中国china";
Pattern p = Pattern.compile("[a-zA-Z]+");
Matcher m = p.matcher(s);
ArrayList<String> words = new ArrayList<String>();
int[] count = new int[s.length()];
String word;
int index;
while(m.find()) {
word = m.group();
index = words.indexOf(word);
if(index==-1) {
words.add(word);
count[words.size()-1] = 1;
}else {
count[index]++;
}
}
char c;
for(int i=0; i<s.length(); i++) {
c = s.charAt(i);
if(c>255) {
index = words.indexOf(c+"");
if(index==-1) {
words.add(c+"");
count[words.size()-1] = 1;
}else {
count[index]++;
}
}
}
for(int i=0; i<words.size(); i++) {
System.out.print(words.get(i)+": ");
System.out.println(count[i]);
}
}}
// TODO Auto-generated method stub
String s = "中国china i love中国china";
ArrayList<String> words = new ArrayList<String>();
int[] count = new int[s.length()];
char c;
StringBuilder sb = new StringBuilder();
boolean flag = false;
for(int i=0; i<s.length(); i++) {
c = s.charAt(i);
if(c>255) {
rememberNode(c+"", words, count);
}
if(c>='a'&&c<='z' || c>='A'&&c<='Z') {
flag = true;
sb.append(c);
}else if(flag == true) {
flag = false;
rememberNode(sb.toString(), words, count);
sb.delete(0, sb.length());
}
}
if(flag == true)
rememberNode(sb.toString(), words, count);
for(int i=0; i<words.size(); i++) {
System.out.print(words.get(i)+": ");
System.out.println(count[i]);
} } public static void rememberNode(String node, ArrayList<String>l, int[] count) {
int index;
index = l.indexOf(node);
if(index==-1) {
l.add(node);
count[l.size()-1] = 1;
}else {
count[index]++;
}
}
}
// mypackage1/Words.java
// 统计字符串中单词出现的次数package mypackage1;import java.util.regex.*;
import java.util.*;public class Words {
public static void main(String[] args) {
String words = "中国china i love中国china";
Map<String, Integer> map = new HashMap<String, Integer>();
Pattern p = Pattern.compile("([a-zA-Z]+|[\u4e00-\u9fa5]+)");
Matcher m = p.matcher(words);
while (m.find()) {
Integer i = map.get(m.group());
map.put(m.group(), i == null ? 1 : i + 1);
}
System.out.println(map);
}
} ///:~
//output:{love=1, i=1, china=2, 中国=2}
//
// 统计字符串中单词出现的次数package mypackage1;import java.util.regex.*;
import java.util.*;public class Words {
public static void main(String[] args) {
String words = "中国china i love中国china";
Map<String, Integer> map = new HashMap<String, Integer>();
Pattern p = Pattern.compile("([a-zA-Z]+|[\u4e00-\u9fa5])");
Matcher m = p.matcher(words);
while (m.find()) {
Integer i = map.get(m.group());
map.put(m.group(), i == null ? 1 : i + 1);
}
System.out.println(map);
}
} ///:~
//output:
//{love=1, 中=2, 国=2, i=1, china=2}