How could I have the index of an array 'roll over' when incrementing?

前端 未结 4 460
刺人心
刺人心 2020-12-20 19:59

So I have an Array with a length of 4. When I increment it by 1 and the number gets bigger than the length of the array, I\'d like it to rollover.

For example:

4条回答
  •  无人及你
    2020-12-20 20:26

    Divide the incremented index modulo the array's length:

    current_index = (current_index + n) % textviewlist.length
    

提交回复
热议问题