title: Java - Insert an element after every n elements in an ArrayList
body: <p>My aim is to create an array of 22 elements in which there must be 11 elements "G" and 11 elements "B". Every 5th element in the array is "B".</p>
  
  <p>That is, the 5th element in the array is B, the 10th is B, the 15th is B and 20th is B. The array should wrap after the last element and skip the existing B elements, hence the next B is the 3rd element, followed by the 9th element and so on.</p>
  
  <p>So far, this is my code and present output:</p>
  
  <pre><code>private static ArrayList&lt;String&gt; circle;
  
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      circle = new ArrayList&lt;String&gt;();
  
      for(int i=0; i&lt; 22; i++){
          circle.add("G");
      }
  
      for(int i=0; i&lt;circle.size(); i++){
          if((i+1) % 5 == 0){
              circle.set(i, "B");             
          }
      }
      for(int i=0; i&lt;circle.size(); i++){
          System.out.printf("%3d", (i+1));
      }
      System.out.print("\n");
      for(int i=0; i&lt;circle.size(); i++){
          System.out.printf("%3s", circle.get(i));
      }
  
  }
  /* Output */
  /*  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
      G  G  G  G  B  G  G  G  G  B  G  G  G  G  B  G  G  G  G  B  G  G */
  </code></pre>
  
  <p>The desired output is</p>
  
  <pre><code>/* Desired Output */
  /*  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
      B  G  B  G  B  B  G  B  G  B  B  G  B  G  B  G  G  B  G  B  G  G */
  </code></pre>
  
  <p>How do I go about it?
  Thanks!</p>