Imagine that there is a collection of integer values. For example, 1 5 8 3 0 15 7 . Is there any method that would move the values ​​to some definitely number? For example, I want to move all values ​​by 2 to the left. Then it will look like this: 8 3 0 15 7 1 5 .

  • four
    For such things, the fifo queue is perfect - yolosora
  • one
    @yolosora sure, thanks! Write it as an answer, I will give you a plus. - Pro100Denysko
  • For certain types of applications, a container based on an array of length n with an offset number d may be quicker. So that the reading of the i th element returns (i+d)%n 'th element of the array. The offset by 2 to the left will be expressed only in a decrease of d by 2 (you can still take the remainder of dividing by n in the result to avoid special effects from large d ) - D-side
  • for collections there is a standard java.utils.Collections.rotate method. For arrays of primitives like no, you can read about possible implementations here . - zRrr pm

1 answer 1

As yolosora wrote above, fifo is suitable for this purpose.