数组索引值增加?我正试图理解这个 – count[str.charAt(i)]++

huangapple 未分类评论44阅读模式
英文:

Array index value increment ?i am trying to understand this - count[str.charAt(i)]++

问题

我对这行代码有疑问,在执行这段代码时,数组大小会是多少:

count[str.charAt(i)]++

英文:

I'm having this doubt in this line of code, what will be array size when this code is executed:

count[str.charAt(i)]++

答案1

得分: -1

"count"是一个数字类型(int、long)的数组吗?

你正在对一个字符串进行索引,盲目获取ASCII字符值,并将其用于索引到另一个数组。

然后进行递增操作?你可能会发现你所递增的不是你想要的东西。你想要做什么?嗯。

所以,像"1"是31,我认为可能不是你想要的。

也许更像是这样:

int idx = Integer.parseInt(are.charAt(t)); //给你1而不是31
count[idx]++;

另外 -

你的数组大小不会改变,但你很有可能会抛出异常。

英文:

Is count an array of a numeric type (int, long)?

You're indexing into a string, getting the ASCII character value blindly, and using it to index into another array.

Then incrementing? You may find that you aren't incrementing what you think you are. What are you trying to do? Hmm.

So, like "1" is 31, which is I think perhaps not what you wanted.

Perhaps something more like

Int idx = Integer.parseInt(are.charAt(t)); //gives you 1 instead of 31
count [idx]++

Also -

YOUR ARRAY SIZE WON'T CHANGE BUT YOU ARE HIGHLY LIKELY TO THROW AN EXCEPTION

huangapple
  • 本文由 发表于 2020年4月9日 13:28:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/61114530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定