Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I do belive that <code>System.arraycopy</code> would actually just take all your data from one array, and put it into another one of the same length just shifted.</p> <p>Anyways thinking about that problem is a quite interesting task. The only Solution i could think about right now is to shit it one by one. Without using another Array it would look like that:</p> <pre><code>for(int i = 0; i &lt; shift;i++) { tmp = array[0]; for(int j = 0;j&lt;array.length-1;j++) array[j]=array[j+1]; array[array.length-1]=tmp; } </code></pre> <p>for Arrays greater than 30 items it is but more efficient to use this:</p> <pre><code>for (int i = 0; i &lt; shift; i++) { tmp = array[0]; System.arraycopy( array, 1, array, 0, array.length - 1 ); array[array.length - 1] = tmp; } </code></pre> <p>But for large arrays and great shift that are close to the array size aswell as for short arrays and small shifts this method wins the race:</p> <pre><code> int[] array2 = new int[shift]; for (int i = 0; i &lt; shift; i++) { array2[i] = array[i]; } System.arraycopy(array, shift, array, 0, array.length - shift); for (int i = array.length - shift; i &lt; array.length; i++) { array[i] = array2[shift + i - array.length]; } </code></pre> <p>Ive tested that with a few array sizes and shifts Here are the results for </p> <pre><code> int[] array = new int[100000]; int shift = 99999; </code></pre> <p>in nanoseconds: 1st method:5663109208 2nd method:4047735536 3rd method:6085690 So you should really use the 3rd method. Hope that helps</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload