[英]Clarification about ArrayList syntax
What is being done in this for
loop, counter
and arr
are two different ArrayLists. 在
for
循环, counter
和arr
中正在做的是两个不同的ArrayLists。
// 3. store count of each number as we iterate through arr
for(int i = 0; i< arr.size(); i++){
counter[arr.get(i)]++;
}
arr
contains indexes that the corresponding elements for them on counter
should be incremented. arr
包含索引,它们应在counter
上递增相应的元素。
For example: 例如:
arr = [1,3,4]
Then the elements in 1,3 and 4 will be incremented in the array counter
. 然后1,3和4中的元素将在数组
counter
递增。
I highly recommend you to debug your code to better understand the flow of the program. 我强烈建议您调试代码以更好地理解程序的流程。 You should also be careful with
ArrayIndexOutOfBoundsException
: 您还应该小心
ArrayIndexOutOfBoundsException
:
for(int i = 0; i< arr.size(); i++) {
if(arr.get(i) < 0 || arr.get(i) >= counter.length) {
continue;
}
counter[arr.get(i)]++;
}
Or doing something else, depends on the logic of your program. 或者做其他事情,取决于你的程序的逻辑。
If arr
contains integer numbers it is index of counter
array. 如果
arr
包含整数,则它是counter
数组的索引。 Then you are incrementing the value of counter
array on arr.get(i)
index. 然后你在
arr.get(i)
索引上递增counter
数组的值。 I hope you understand what I am saying. 我希望你明白我在说什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.