疯狂java讲义习题7.3

xiaoxiao2021-02-28  108

题目描述: 给定["a","b","a","b","c","a","b","c","b"]字符串数组,然后使用Map的Key来保存数组中字符串元素,value保存该字符串出现的次数,最后统计出各字符串元素的出现次数 代码如下:

点击(此处)折叠或打开

import java.io.*; import java.util.*; public class cmd {     public static void main(String args[])     {         String str[] = {"a","b","a","b","c","a","b","c","b"};         HashMap hm = new HashMap();         int count1 = 0,count2 = 0,count3 = 0;         for (int i = 0;i < str.length;i++)         {                          if (str[i].equals("a"))             {                                  count1++;             }             else if (str[i].equals("b"))             {                 count2++;             }             else if (str[i].equals("c"))             {                 count3++;             }             else{}         }                  hm.put("a",count1);         hm.put("b",count2);         hm.put("c",count3);         System.out.println(hm);                       }      } 运行结果如下: {b=4, c=2, a=3} <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> 阅读(105) | 评论(0) | 转发(0) | 0

上一篇:疯狂java讲义习题7.2

下一篇:疯狂java讲义习题7.4

相关热门文章 Tomcat 6 配置SSI让Resin支持shtml(SSI)- 静...tomcat + ssiASP JavaScript Lessons(8-14)JDK1.6官方下载_JDK6官方下载_... 给主人留下些什么吧!~~ 评论热议
转载请注明原文地址: https://www.6miu.com/read-59831.html

最新回复(0)