<div><div>I __believe__ that insertInPlace doesn't shift the elements, but use an appender allocating another array instead.</div><div>Maybe this function do what you want.</div><div><font face="'courier new', monospace"><br>

</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">    int[] arr = [0,1,2,3,4,5,6,7,8,9];</font></div><div><font face="'courier new', monospace">    </font></div>

<div><font face="'courier new', monospace">    void maybe(T)(T[] arr, size_t pos, T value) {</font></div><div><font face="'courier new', monospace">        size_t i;</font></div><div><font face="'courier new', monospace">        for (i = arr.length - 1; i > pos; i--) {</font></div>

<div><font face="'courier new', monospace">            arr[i] = arr[i-1];</font></div><div><font face="'courier new', monospace">        }</font></div><div><font face="'courier new', monospace">        arr[i] = value;</font></div>

<div><font face="'courier new', monospace">    }</font></div><div><font face="'courier new', monospace">    </font></div><div><font face="'courier new', monospace">    maybe(arr, 3, 0);</font></div>

<div><font face="'courier new', monospace">    maybe(arr, 0, 1);</font></div><div><font face="'courier new', monospace">    assert(arr == [1, 0, 1, 2, 0, 3, 4, 5, 6, 7]);</font></div></div><div><br></div>

<br><br><div class="gmail_quote">2012/2/9 MattCodr <span dir="ltr"><<a href="mailto:matheus_nab@hotmail.com">matheus_nab@hotmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

I have a doubt about the best way to insert and move (not replace) some data on an array.<br>
<br>
For example,<br>
<br>
In some cases if I want to do action above, I do a loop moving the data until the point that I want and finally I insert the new data there.<br>
<br>
<br>
In D I did this:<br>
<br>
begin code<br>
.<br>
.<br>
.<br>
   int[] arr = [0,1,2,3,4,5,6,7,8,9];<br>
<br>
   arr.insertInPlace(position, newValue);<br>
   arr.popBack();<br>
.<br>
.<br>
.<br>
end code<br>
<br>
<br>
After the insertInPlace my array changed it's length to 11, so I use arr.popBack(); to keep the array length = 10;<br>
<br>
The code above is working well, I just want know if is there a better way?<br>
<br>
Thanks,<br>
<br>
Matheus.<br>
</blockquote></div><br>