Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>`The problem is that you're overwriting your indexes and at end, you come to same values that you've been setting in the beginning. For your case, the programs works like this:</p> <pre class="lang-java prettyprint-override"><code>num[4] -&gt; num[0] -- 8 overwrites 55, num = {8,2,37,9,8} num[3] -&gt; num[1] -- 9 overwrites 2, num = {8,9,37,9,8} num[2] -&gt; num[2] -- 37 gets set to same index, num = {8,9,37,9,8} num[1] -&gt; num[3] -- 9 gets set to overwrited value of 2 (now 9), num = {8,9,37,9,8} num[0] -&gt; num[4] -- 8 gets set to overwrited value of 55 (now 8), num = {8,9,37,9,8} </code></pre> <p><strong>The most simple solution to understand is</strong> <em>(using your code)</em>:</p> <pre class="lang-java prettyprint-override"><code>int reversedArray[] = new int[num.Length]; for (int i = 1 ; i != 5; i++) { reversedArray[i - 1] = num[num.length - i]; } </code></pre> <p>And some tips on your code:</p> <ul> <li>use <code>{object type}[] array = ...</code> instead of <code>{object type} array[]</code>. Although it works, if you're ever going to transfer to C#, this might give you problems. On the other hand, this <em>is</em> correct syntax for c/c++.</li> <li>in <code>for</code> loop, instead of initializing <code>i</code> at 1, start it at 0 as the all arrays are zero-based (this means the first index has a number of 0). This way you can index array only with <code>i</code>, not having to additionally subtract it. </li> <li>Also, for the condition, use <code>i &lt; array.Length</code>. This will end 1 number before the number you've put in. For your example, it would loop from 0 to 4 and when it hits 5, it won't repeat the loop again. Again, your syntax works, but the other one is more logical to understand. Also, it might happen (in more complex program scenarios) that looping this way will not give you the result expected, causing you huge headaches because of very simple error.</li> </ul> <p><strong>A more correct solution</strong> <em>(applying my tips to your code)</em>:</p> <pre class="lang-java prettyprint-override"><code>int[] reversedArray = new int[num.Length]; for (int i = 0 ; i &lt; num.Length; i++) { reversedArray[i] = num[num.length - i]; } </code></pre> <p><em>I see that in the time I was writing this, some new questions were added, but I hope that mine helped you understand what was your error and why did it happen.</em></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. 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