Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following rewriting should be instructive:</p> <pre><code>public class Delete { static int search(int key, int[] arr) { for (int i = 0; i &lt; arr.length; i++) if (arr[i] == key) { return i; } return -1; } static void print(int[] arr, final int L) { for (int i = 0; i &lt; L; i++) { System.out.println(arr[i]); // try this also: // System.out.format("%02d ", arr[i]); } } public static void main(String[] args) { int nums[] = { 77, 99, 44, 11, 00, 55, 66, 33, 10 }; final int N = nums.length; int searchKey = 55; int pos = search(searchKey, nums); for (int t = pos; t &lt; N-1; t++) { nums[t] = nums[t + 1]; } print(nums, N-1); // prints 77, 99, 44, 11, 0, 66, 33, 10 System.out.println(010 == 8); // prints "true" System.out.println(00000); // prints "0 } } </code></pre> <p>Here are some key observations:</p> <ul> <li>Break apart logic into helper methods. This makes the logical components easier to test and reuse, and the overall logic easier to understand.</li> <li>It makes the code easier to understand if you use <code>final</code> local variables like <code>N</code> to denote the initial size of <code>int[] nums</code>, and define the rest of the logic in terms of <code>N</code>, <code>N-1</code>, etc. <ul> <li>The more non-<code>final</code> variables there are, the harder it is to understand what's going on as their values changes over time</li> </ul></li> <li>Follow <a href="http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html" rel="nofollow noreferrer">coding convention</a>. In particular, class names starts with uppercase.</li> <li>Do be careful with the <code>00</code> in the array. The <code>0</code> prefix is for octal literals. That is, <code>010 == 8</code>.</li> <li>Do note that <code>00</code> is printed as simple <code>0</code>. Numerically, <code>00 = 000 = 0000 = 0</code>. If you need this to be zero-padded, then that's a formatting issue.</li> </ul> <hr> <h2>See also</h2> <h3>On octal literals</h3> <ul> <li><a href="https://stackoverflow.com/questions/970039/09-is-not-recognized-where-as-9-is-recognized">09 is not recognized where as 9 is recognized</a></li> <li><a href="https://stackoverflow.com/questions/565634/integer-with-leading-zeroes">Integer with leading zeroes</a></li> </ul> <h3>On zero-padding</h3> <ul> <li><a href="https://stackoverflow.com/questions/473282/left-padding-integers-with-zeros-in-java">Left padding integers with zeros in Java</a></li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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