package com.company;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created by ttc on 16-10-28.
*/
public class map练习
{
public static void main(String[] args)
{
System.out.println("请输入一段英文");
Scanner sc = new Scanner(System.in);
String st=sc.nextLine();
String[] str= st.split(" ");
Map<String,Integer> map = new HashMap<String,Integer>();
//遍历单词数组
for(String s :str)
{
if(!map.containsKey(s))
{
map.put(s,1);
}
else
{
Integer i =map.get(s);
i++;
map.put(s,i);
}
}
for(Map.Entry<String,Integer> me: map.entrySet())
{
String strKey = me.getKey();
Integer iCount = me.getValue();
System.out.println(strKey+"出现了"+iCount+"次");
}
}
}
map 遍历练习 每个单词出现了几次
最新推荐文章于 2021-02-25 05:41:47 发布