Overriding through an array
3 answers
If you want to cycle through the array in a circular fashion, then the module %
is the path. If you combine this with Java 8 Streaming API: s the solution might look like this.
int[] myArray = {1, 2, 3, 4, 5}; // Setup the array
int startIndex = 3; // Start index as per the OP example
int addIndex = 4; // Add as per the OP example
// Loop via streams - no external looping
IntStream.range(startIndex, startIndex + addIndex)
.map(i -> myArray[i % myArray.length]) // circular loop
.forEach(System.out::println /* Do your stuff */); // do your stuff
For more examples see this answer
+1
source to share