统计字符出现的次数和出现的位置|JAVA

统计字符出现的次数和出现的位置

  • 这个题写的并不顺利,java果然还是没有python方便
  • 因为要统计字母的个数,我又不想用swtich语句,所以用到了HashMap,这个类似于python当中的字典,values的值为arrayList,在各种数据转换中卡住了。
  • 最后用了强制转换什么的做出来了
    在这里插入图片描述
public static void lookup(String str)
	{
		ArrayList loation = new ArrayList();
		Map countNum = new HashMap(); 
		Map charLocationMap = new HashMap(); 
		char temp;
		char[] tempList=str.toCharArray(); //将字符串截取成字符数组
		for (int i = 0; i < tempList.length; i++) 
		{
			 temp = tempList[i];
			 if(charLocationMap.get(temp)==null)
			 {
				 ArrayList charLocList = new ArrayList();
				 charLocList.add(i);
				 charLocationMap.put(temp,charLocList);
			 }
			 else
			 {
				 List<Integer> arrList = (ArrayList<Integer>)charLocationMap.get(temp);
				 arrList.add(i);
				 charLocationMap.put(temp,arrList);
			 }
		}
		System.out.print(charLocationMap);
		ArrayList NumberCount = new ArrayList(charLocationMap.keySet());
		for (int i = 0; i < NumberCount.size(); i++)
		{
			char tem = (char)NumberCount.get(i);
			List<Integer> arrList = (ArrayList<Integer>)charLocationMap.get(tem);
			System.out.printf("%s的个数是:%d\n",tem,arrList.size());
		}

	}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值