java 中Map问题
读取一个由英文字母组成的文件,利用Map<Character,Integer>创建一个程序,它可以对一个文件中所有不同的字符出现次数进行计数,(如a出现12,则在Map中...
读取一个由英文字母组成的文件,利用Map<Character,Integer>创建一个程序,它可以对一个文件中所有不同的字符出现次数进行计数,(如a出现12,则在Map中包含a的Character相关联的Integer就包含12)
展开
1个回答
展开全部
怎么也不给点分啊,应该就是这个程序:希望对你有用
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
public class Test8 {
public static void main(String[] args){
String s="One of the first computerlike devices " +
"was developed in Germany by Konrad Zuse in 1941." +
" Called the Z3, it was general-purpose, " +
"stored-program machine with many electronic parts," +
" but it had a mechanical memory. ";
StringTokenizer st=new StringTokenizer(s," ,.");
HashMap<String, Integer> hm=new HashMap<String, Integer>();
while(st.hasMoreElements()){
String key=(String)st.nextElement();
if(hm.containsKey(key)){
hm.put(key,hm.get(key)+1);
}else{
hm.put(key, 1);
}
}
Set<String> words=hm.keySet();
Iterator<String> it=words.iterator();
while(it.hasNext()){
String word=(String)it.next();
System.out.println(word+":"+hm.get(word));
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
public class Test8 {
public static void main(String[] args){
String s="One of the first computerlike devices " +
"was developed in Germany by Konrad Zuse in 1941." +
" Called the Z3, it was general-purpose, " +
"stored-program machine with many electronic parts," +
" but it had a mechanical memory. ";
StringTokenizer st=new StringTokenizer(s," ,.");
HashMap<String, Integer> hm=new HashMap<String, Integer>();
while(st.hasMoreElements()){
String key=(String)st.nextElement();
if(hm.containsKey(key)){
hm.put(key,hm.get(key)+1);
}else{
hm.put(key, 1);
}
}
Set<String> words=hm.keySet();
Iterator<String> it=words.iterator();
while(it.hasNext()){
String word=(String)it.next();
System.out.println(word+":"+hm.get(word));
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询