title: How can I merge elements of an array in a loop in JAVA?
body: <p>If an array contains the same number in adjacent positions, they are merged into a single number like in the example input and output below with double that value.</p>
  
  <pre><code>int[] test = {2,0,2,4,0,0,4};
  Tile.alter(test)
  true
  test
  { 4, 8, 0, 0, 0, 0, 0 } //(the two 2’s merged, and the two 4’s merged)
  int[] test = {2,0,4,2,0,0,4};
  Tile.alter(test)
  true
  test
  { 2, 4, 2, 4, 0, 0, 0 } //(nothing merged – the 2’s are not adjacent, nor are the 4’s)
  </code></pre>
  
  <p>I tried this and its not working:</p>
  
  <pre><code>int curr=size[i];
  if(size.length&lt;=0)
  int prev = size[0];
  size[0]=size[0]*size[1];
  for(int j = 0;j&lt;size.length;j++)
  size[j]=prev*size[j+1];
  prev=curr;
  size[size.length-1]=prev *size[size.length-1];
  </code></pre>