package co.hp.xs;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class xs{
public static void main(String[] args) {
String[] name = new String[]{"张三","李四","张恰","刘能","王大拿","王小二","张志刚","刘星","李百万","李星"};
Map map = new HashMap<>();
String zhang = null;
String wang = null;
String li = null;
String liu = null;
int num = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
for (int i =0 ; i<name.length;i++) {
if (name[i].contains("张")){
zhang = name[i];
num++;
}
if (name[i].contains("李")){
li = name[i];
//System.out.println(zhang.charAt(0));
num1++;
}
if (name[i].contains("刘")){
liu = name[i];
//System.out.println(zhang.charAt(0));
num2++;
}
if (name[i].contains("王")){
wang = name[i];
//System.out.println(zhang.charAt(0));
num3++;
}
}
map.put(zhang.charAt(0),num);
map.put(li.charAt(0),num1);
map.put(liu.charAt(0),num1);
map.put(wang.charAt(0),num1);
System.out.println(map);
}
}
使用Map集合来做一个不同姓氏人数的统计 有一个String数组保存着10个人的姓名{“张三“,“李四“,“王二“...} 通过程序设计,把不同姓氏的姓氏和人数保存到Map集合中
最新推荐文章于 2023-09-05 09:36:50 发布