题目描述:
给定["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}
阅读(1625) | 评论(0) | 转发(0) |