统计单词个数以及倒序输出

来源:互联网 发布:centos无线网卡配置 编辑:程序博客网 时间:2024/06/04 18:20
public class EnglishNum {    //统计单词个数      public static void main(String[] args){            String words = "Look buddy, U got work hard and put  yourself in your java, Once you learned the heart of the java, I can guarantee that you win.";            String reg = "[a-zA-Z]+";            Pattern p = Pattern.compile(reg);            Matcher m = p.matcher(words);            HashMap<String, Integer> map = new HashMap<String, Integer>();            int count = 0;            while(m.find()){                    count++;                    String w = m.group();                if(null == map.get(w)){                    map.put(w, 1);                }else{                    int x = map.get(w);                    map.put(w, x + 1);                }            }            System.out.println(count);            System.out.println(map);        }        }
    //倒序进行输出    public static void main(String [] args){        String words = "I come from China.";//      String[] m =words.split("\\s");//      for(int j =m.length-1;j>=0;j--){//          System.out.print(m[j]+" ");//      }        Stack<String> stack = new Stack<>();        for(String s:words.split(" ")){            stack.push(s);        }        while(!stack.empty()){            System.out.print(stack.pop()+" ");        }    }
0 0