疯狂java讲义习题7.3

来源:互联网 发布:百度软件助手怎么样? 编辑:程序博客网 时间:2024/05/29 17:29
题目描述:
给定["a","b","a","b","c","a","b","c","b"]字符串数组,然后使用Map的Key来保存数组中字符串元素,value保存该字符串出现的次数,最后统计出各字符串元素的出现次数
代码如下:

点击(此处)折叠或打开

  1. import java.io.*;
  2. import java.util.*;
  3. public class cmd {
  4.     public static void main(String args[])
  5.     {
  6.         String str[] = {"a","b","a","b","c","a","b","c","b"};
  7.         HashMap hm = new HashMap();
  8.         int count1 = 0,count2 = 0,count3 = 0;
  9.         for (int i = 0;i < str.length;i++)
  10.         {
  11.             
  12.             if (str[i].equals("a"))
  13.             {
  14.                 
  15.                 count1++;
  16.             }
  17.             else if (str[i].equals("b"))
  18.             {
  19.                 count2++;
  20.             }
  21.             else if (str[i].equals("c"))
  22.             {
  23.                 count3++;
  24.             }
  25.             else{}
  26.         }
  27.         
  28.         hm.put("a",count1);
  29.         hm.put("b",count2);
  30.         hm.put("c",count3);
  31.         System.out.println(hm);
  32.         
  33.         
  34.     }
  35.     
  36. }
运行结果如下:
{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 + ssi
  • ASP JavaScript Lessons(8-14)
  • JDK1.6官方下载_JDK6官方下载_...
给主人留下些什么吧!~~
原创粉丝点击