Java – reload via array

So suppose I have an array of length 5 (let's call it myArray [5]) If I add 4 to myArray [3], such as myArray [3, 4], how can I make it cycle through myArray again so that it becomes myArray [2]?

(example)

myArray[3]

myArray[4]   //+1

myArray[0]   //+2

myArray[1]   //+3

myArray[2]   //+4

Solution

Simply use the array length for modulo operation:

int index = /* index you are using */;
myArray[index % myArray.length]
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>