赞
踩
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
-
-
- public class SubStringAppear
- {
- public static void main(String args[])
- {
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- String str=null;
- try {
- str = br.readLine();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- int a=0,b=0;
- ArrayList list=new ArrayList();
- int i,j;
- while(true)
- {
- for(i=b;i<str.length();i++)
- {
- if(str.charAt(i)!=' ')
- {
- a=i;
- break;
- }
- }
- for(j=a;j<str.length();j++)
- {
- if(str.charAt(j)==' ')
- {
- b=j;
- break;
- }
- }
- if(i<str.length()&&j<str.length())
- {
- list.add(str.substring(a, b));
- }
- if(i<str.length()&&j==str.length())
- {
- list.add(str.substring(a,str.length()));
- break;
- }
- if(i==str.length())
- {
- break;
- }
- }
- Iterator<String> ite=list.iterator();
- while(ite.hasNext())
- {
- System.out.println(ite.next());
- }
- HashMap<String,Integer> map=new HashMap<String,Integer>();
- ite=list.iterator();
- while(ite.hasNext())
- {
- String s=ite.next();
- if(map.get(s)==null)
- {
- map.put(s, 1);
- }
- else
- {
- map.put(s, new Integer(map.get(s)+1));
- }
- }
- Collection col=map.values();
- Iterator<Integer> io=col.iterator();
- int max=0;
- while(io.hasNext())
- {
- int k=io.next();
- if(max<k)
- {
- max=k;
- }
- }
- Set<Map.Entry<String, Integer>> set=map.entrySet();
- Iterator<Map.Entry<String, Integer>> it=set.iterator();
- while(it.hasNext())
- {
- Map.Entry<String , Integer> entry=it.next();
- if(entry.getValue()==max)
- {
- System.out.println("出现次数最多的单词为: "+entry.getValue()+"出现的次数为: "+max);
- }
- }
-
- }
- }
- //用java的迭代器和HashMap实现的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。