m0_73810475 2022-10-13 18:27 采纳率: 0%
浏览 203

给定一个字符串数组,从字符串数组words中解析出每个单词存放到wordMap中,同时统计单词数量。将HashMap修改为TreeMap,再次运行程序

给定一个字符串数组,内容如下:

String[] words = {"no pains, no gains."

"well begun is half done.",

" where there is a will, there is a wa

编写程序,创建一个HashMap对象,它的键是英文单词,值是该单词的个数。

HashMap<String, Integer> wordMap = new HashMap<>();

从字符串数组words中解析出每个单词存放到wordMap中,同时统计单词数量。将HashMap修改为TreeMap,再次运行程序,结果如下:
共有13个不同的单词 {no=2, a=2, half=1, will=1, begun=1, pains=1, is=3, gains=1, done=1, way=1, there=2, well=1, where=1} {a=2, begun=1, done=1, gains=1, half=1, is=3, no=2, pains=1, there=2, way=1, well=1, where=1, will=i}

  • 写回答

1条回答 默认 最新

  • a5156520 2022-10-14 10:50
    关注

    我的思路如下:
    1、首先用第一个for循环遍历字符串数组wordMap,进行单词计数计算。

    2、用第二个for循环遍历当前位置的字符串的按空格分解数组,如果是单词,则进行单词计数操作。单词计数操作,先判断是否存在于HashMap,如果存在,则在原来的计数上+1,如果第一次出现,则计数为1

    3、打印结果

    4、把上面的代码复制出来,把HashMap改为TreeMap,然后修改对应的变量名。

    参考链接:
    java中怎么搜索一个字符串中的所有英文单词?_百度知道
    Java之hashMap遍历方式_ldcaws的博客-CSDN博客_java遍历hashmap

    代码如下:

    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.TreeMap;
    import java.util.regex.Pattern;
    
    public class CountWordsNums {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String[] words = {"no pains, no gains.",
    
                    "well begun is half done.",
    
                    " where there is a will, there is a way"};
            
            System.out.println("使用HashMap:");
            //https://zhidao.baidu.com/question/982412208210421459.html
            HashMap<String,Integer> wordMap = new HashMap<>();
            for(int i=0;i<words.length;i++) { //遍历字符串数组
                
                String s = words[i];  //获取当前位置字符串
                String [] strs = s.split("\\b");  //按空格分割,按空格分解字符串
                //https://www.codenong.com/8248277/
                Pattern p = Pattern.compile("[a-zA-Z]"); //判断字符串是否由字母组成的正则式
                for(String w:strs) { //遍历当前字符串按空格分解后的每个字符串数组
                    
                    boolean chars = p.matcher(w).find(); //检测当前的字符串w是字母组成的字符串
                    //System.out.println("w="+w+",specialChar="+chars);
                    if (chars==true) { //如果是字母组成的字符串,则进行计数操作
                        if(wordMap.containsKey(w)) { //如果这个单词在wordMap存在,则在原来的计数上+1
                            wordMap.put(w, wordMap.get(w)+1);
                        }else {  //如果是第一次出现,计算为1
                            wordMap.put(w, 1);
                        }
                    }
                }            
            }
            
            //https://blog.csdn.net/leijie0322/article/details/123023309
             //打印结果
            for(Entry<String,Integer> entry:wordMap.entrySet()) {
                System.out.println(entry.getKey()+":"+entry.getValue());
            }
            
            System.out.println("\n使用TreeMap:");
            //TreeMap的方式同上,只把数据类型和变量名修改了一下
            TreeMap<String,Integer> wordTree = new TreeMap<>();
            for(int i=0;i<words.length;i++) {
                
                String s = words[i];
                String [] strs = s.split("\\b");
                //https://www.codenong.com/8248277/
                Pattern p = Pattern.compile("[a-zA-Z]");
                for(String w:strs) {
                    
                    boolean chars = p.matcher(w).find();
                    //System.out.println("w="+w+",specialChar="+chars);
                    if (chars==true) {
                        if(wordTree.containsKey(w)) {
                            wordTree.put(w, wordTree.get(w)+1);
                        }else {
                            wordTree.put(w, 1);
                        }
                    }
                }            
            }
            
            //https://blog.csdn.net/leijie0322/article/details/123023309
            for(Entry<String,Integer> entry:wordTree.entrySet()) {
                System.out.println(entry.getKey()+":"+entry.getValue());
            }
            
        }
    
    }
    
    

    img

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 10月13日