Create a circular buffer

How can I index an array using a circular index that wraps around to the first index when I reach the end of the array?
You can create a circular buffer by using the modulus operator. The following sample code shows how to increment the index and automatically make it wrap around when it reaches the end:
var numbers = [1,2,3,4]; //Array with arbitrary content int currentIndex = 0; //Circular index currentIndex = (currentIndex + 1) % numbers.length; //Creates wrap around effect when it reaches the end
programming
Check out more questions and answers here