Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just one potential problem:</p> <pre><code>for(int i = 0; i &lt; numbers.size(); i++){ ... if(product == false){ numbers.remove(i); } } </code></pre> <p>This might skip numbers. Consider the list <code>N,P,*</code> (where <code>N</code> is a non-product palidrome, <code>P</code> is a product palindrome and <code>*</code> is any palindrome). i is 0 and since N is a non-product palidrome it will be removed and your list now is <code>P,*</code>. Now i will be increased to 1 and thus the i-th element is <code>*</code>. <code>P</code> will be skipped - ouch.</p> <p>To fix that, you might collect the palindromes into another set/list/collection and leave <code>numbers</code> unchanged.</p> <p>Alternatively iterate backwards, i.e. <code>for( i = numbers.size(); i &gt;= 0; i--)</code>.</p> <p>A thirds option would be to use an iterator, e.g. <code>for( Iterator&lt;Integer&gt; itr = numbers.iterator(); itr.hasNext(); ) { ... }</code> and then <code>itr.next()</code> and <code>itr.remove()</code>.</p> <p>Btw, you might want to use for a foreach loop whenever the value of <code>i</code> is not relevant, e.g. <code>for(Integer number : numbers )</code></p> <p><strong>Edit</strong>: changed the example from <code>10,11,12</code> to <code>65,66,67</code> to reduce confusion. Please note that it's still an example and not necessarily based on your actual data.</p> <p><strong>Edit 2</strong>: I changed the example to something more abstract to avoid (or generate? ;) ) further confusion. Since I can't currently think of a sequence of a non-product palindrome followed by a product palindrome (<code>product</code> here means matching the requirement of being a product of 2 one- or two-digit numbers), I changed it to <code>N,P,*</code>.</p> <p>I'll restate the point of my <em>potential bug</em> answer: when you iterate forward using indices and remove elements at the current or a lower index, you would skip elements, so don't do that unless you want that exact behavior.</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.
 

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