if ((s.charAt(i)+"").equals("a")) {
// 累加统计次数
num++;
}
这里的s.charAt(i)运行后本来就是字符串,为什么要加一个”“再去.equals("a")?
下面是这个方法的API,因为返回值是char而不是String,而后面的''a"是String类型,所以需要类型转换,这里加上“”可向上转换成String.就省去了强制转换。本人也是新手,也许有错,希望大家莫怪。
char charAt(int index)
Returns the char value at the specified index.
"a"是String类型,需要将char转化成String,后面加""是一种方式