title: Sliding Window: How to fill a window correctly? (JAVA/ArrayList+Array)
body: <p>A small part of my homework requires the implementation of sliding window. In other words, if I have a Message array of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, etc] I should fill out the window array depending on the size. 
  In the code below, the window size is mentioned as Max_Size. So assuming my Max_Size is 2, the Window should be filled with [1, 2] then [3, 4] then [5, 6] and so on. 
  The code doesn't iterate correctly (so if I use the command twice, I get the same content) and the information isn't transferring correctly between the two arrays. I haven't coded anything in a long time, so any help is welcome. </p>
  
  <pre><code>    `void fillWindow() {
      ArrayList &lt;Integer&gt; temp = new ArrayList &lt;&gt; (data.size());
      temp.addAll(data);
      switch (Max_Size) {
          case (1) :
              for (int i = 0; i &lt;= Max_Size; i++) {
                  Window.add(i, temp.get(i));
                  temp.remove(i);
                  next_S++;
              }
              break;
          case (7) :
              for (int i = 0; i &lt;= Max_Size; i++) {
                  Window.add(i, temp.get(i));
                  next_S++;
                  if (Window.size() == Max_Size) {
                      for (int j = 0; j &lt; 7; j++) {
                          temp.remove(j);
                      }
                  }
              }
              break;
          case (127) :
              for (int i = 0; i &lt;= Max_Size; i++) {
                  Window.add(i, temp.get(i));
                  next_S++;
                  if (Window.size() == Max_Size) {
                      for (int j = 0; j &lt; Max_Size; j++) {
                          temp.remove(j);
                      }
                  }
              }
              break;
      }
      System.out.println("Window Content = " + Window);
      Window.clear();
  }`
  </code></pre>
  
  <p>My problem is as follows: first, I can't get it to iterate correctly; in other words, it continues to print out the same content every time. My second problem is that the information from the message is transferred incorrectly to the window array. 
  So using the example mentioned above, if Max_Size = 7, instead of printing out [1, 2, 3, 4, 5, 6, 7] it print out something like [1, 2, 3, 4, 5, 8, 9]. So I guess my question is as follows: what did I do wrong, and how can I fix it?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>    void fillWindow() {
      ArrayList &lt;Integer&gt; temp = new ArrayList &lt;&gt; (data.size());
      temp.addAll(data);
      int count = 0;
      while (count &lt; Max_Size) {
          Window.add(count, temp.get(count));
          count++;
      }
      for (int i = 0; i &lt; count; i++) {
          temp.remove(i);
      }
      System.out.println("Window = " + Window);
      Window.clear();
  }
  </code></pre>
  
  <p>After reading @Andreas's comment, this is what I have come up with. The code now iterates correctly, but when I use fillWindow() twice at the same run, it still continues showing the same content. 
  So for example when I do the following:</p>
  
  <pre><code>public static void main(String args[]) 
  { 
          fillWindow();
          fillWindow();
  
  } 
  </code></pre>
  
  <p>and the expected answer is [1, 2, 3] then [4, 5, 6] but instead what I get is [1, 2, 3] and [1, 2, 3]. Is that normal?</p>