add and increment array elements by arraycopy
<p>suppose an array of object: Element T[ ] , each object contains two integer variables (x,y).</p>

<pre><code>T = (1,1) (2,2) (3,3) (4,4)
</code></pre>

<p>I want to increment the value of the variable <strong>x</strong> of object each time a new element is added to array <strong>with the faster way possible</strong> . The new element can be added in any position and we increment all <strong>x</strong> element after the insertion position (position +1)  </p>

<p>Before add (6,6)  :</p>

<p><code>T = (1,1) (2,2) (3,3) (4,4)</code></p>

<p>After add (6,6) in different positions:</p>

<p>1) T = <strong>(6,6)</strong> (<em>2</em>,1) (<em>3</em>,2) (<em>4</em>,3) (<em>5</em>,4)</p>

<p>or </p>

<p>2)  T = (1,1) (2,2) <strong>(6,6)</strong> (<em>4</em>,3) (<em>5</em>,4)</p>

<p>or</p>

<p>3)  T = (1,1) (2,2) (3,3) <strong>(6,6)</strong> (<em>5</em>,4)</p>

<p>I used the method <strong>arraycopy</strong> to add the new element, and loop <strong>for</strong> to increment the variable <strong>x</strong> for each element, as follow:</p>

<ol>
<li><p>increment all <code>x</code> of object elements with loop for</p></li>
<li><p><code>Ta[0] = (6,6)</code></p></li>
<li><p><code>araycopy(T, 0, Ta, 1, T.size-1 );</code> </p></li>
</ol>

<p>because it is faster than </p>

<pre><code>While (i&lt; T.length){

  T[i] = T[i+1]

  T[i].x ++;

   i++;
}
</code></pre>

<p>I need to add the new element and increment the other objects of array simultaneously with a faster time. </p>

<p>//-------------------</p>

<p>public class elemt {</p>

<pre><code>public int x;
public int y;

public elemt(int a, int b){
    this.x= a;
    this.y= b;
}


public void inc(){
 x++;
}

int getX(){
    return x;
}

int getY(){
    return y;
}
</code></pre>

<p>}</p>

<p>//----------------</p>

<p>public class TAD {</p>

<p>public static ArrayList &lt; elemt >  T = new ArrayList &lt; elemt > ( );</p>

<p>public static ArrayList &lt; elemt > T1 = new ArrayList &lt; elemt > ( );</p>

<pre><code> public static void main(String[] args){



    for(int i=0; i&lt;10000000; i++){
       T1.add(new elemt(i, i));
      }


     long t0 = System.currentTimeMillis();

      T1.add(0, new elemt(1, 1));

     long t1= System.currentTimeMillis()- t0;

     System.out.println("Time without Incrementation : "+t1);


 //--------------

     for(int i=0; i&lt;10000000; i++){
       T.add(new elemt(i, i));
      }


    long t2 = System.currentTimeMillis();

       T.add(0, new elemt(1, 1));

        for(int i=1; i&lt;T.size(); i++){
          T.get(i).inc();
        }

    long t3= System.currentTimeMillis()- t2;

  System.out.println("Time with Incrementation: "+t3);



 }
</code></pre>

<p>//------- The results:</p>

<p>Time without Incrementation : 15 ms</p>

<p>Time with Incrementation: 156 ms</p>

<p>My objective is to minimize as possible the time of incrementation process </p>

<p>(Time with Incrementation &lt; Time without Incrementation * 2 )</p>

<p>because actually</p>

<p>Time with Incrementation (156 ms) =  Time without Incrementation (15 ms )* 10  </p>

<p>I notes that i can added a new element in any position, but i chose the worst case (adding an element in the first position that requires the incrementation of all <strong>x</strong> element of the arraylist) </p>
