统计字符出现的次数和出现的位置
- 这个题写的并不顺利,java果然还是没有python方便
- 因为要统计字母的个数,我又不想用swtich语句,所以用到了HashMap,这个类似于python当中的字典,values的值为arrayList,在各种数据转换中卡住了。
- 最后用了强制转换什么的做出来了
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3f7f8960c509df3747337dda6fa5b5d9.png)
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());
}
}