Whats "Ring Buffer"

Ring Buffer is basically like Queue. The difference between Ring Buffer and normal Queue is that Ring Buffer is circular data structure. So, if tail and head index reach the index of max size, they are reseted zero. Therefore we can save memory because we can reuse the space which was using already in array.

Implementation of "Ring Buffer" in Java

The points of implementation.

using "tail" and "head" in order to control the index which we should insert or get

We can define the "tail" and "head" like bellow

  • tail : the index of inserting object in array at next time
  • head: the index of getting object in array at next time

calculating the index by using remainder

By calculating the remainder obtained by dividing head or tail by size of array, we can have the index circulate.