• 写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”)


    public class Test {  

     

        public void index() { 

          String strWords = "Hello World My First Unit Test";

         

          String[] words_Array = strWords.split(" ");

          Map<String,Integer> words_Map=new HashMap<String, Integer>();

          int arrLength = words_Array.length;

          for(int i=0;i<arrLength;i++){

             if(!words_Map.containsKey(words_Array[i])){

                words_Map.put(words_Array[i], 1);

                System.out.println(words_Array[i]);

                System.out.println("出现");

                System.out.println(words_Map.put(words_Array[i], 1));

                System.out.println("次");

               

             }else{

                int currentNum = words_Map.get(words_Array[i])+1;

                words_Map.remove(words_Array[i]);

                words_Map.put(words_Array[i], currentNum);

                System.out.println(words_Array[i]);

                System.out.println("出现");

                System.out.println(words_Map.put(words_Array[i], currentNum));

                System.out.println("次");

             }

    }

    }

    }  

  • 相关阅读:
    用于表示socket的结构体
    Parcelable与Serializable接口的用法和区别
    java类初始化顺序
    孙卫琴java面向对象编程学习笔记
    linux档案权限
    js弹出模态与非模态页面
    ubuntu开启默认的root用户
    java开发实战学习笔记1
    JQuery ajax回调函数
    hadoop命令
  • 原文地址:https://www.cnblogs.com/qiutest/p/5319324.html
Copyright © 2020-2023  润新知