Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've got <code>Comparable</code>, so if we has a <code>zero</code>, <code>a[0]&gt;0</code> could be replaced by <code>a[0].compareTo(zero) &gt; 0</code> (no, I think it's <code>&lt; 0</code>, I can never remember). But now we've run out of useful stuff from <code>Double</code> and the like.</p> <p>If <code>Double</code> and friends weren't <code>java.lang.Comparable</code> we could supply our own <code>java.util.Comparator</code>. That is we take the implementation of comparison outside of the data object. We can also do that addition and multiplication.</p> <pre><code>public interface Testor&lt;T&gt; { // Choose a better name... boolean isStriclyPositive(T value); T add(T lhs, T rhs); T square(T value); } static final Testor&lt;Integer&gt; INTEGER_TESTOR = new Testor&lt;&gt;() { // Java SE 7 diamond. public boolean isStriclyPositive(Integer value) { return value &gt; 0; } public Integer add(Integer lhs, Integer rhs) { return lhs + rhs; } public Integer square(Integer value) { return value*value; } } // ... same for Double, etc.; can't use an enum :( ... test(new Integer[] { 42, 1972, 86 }, INTEGER_TESTOR); public &lt;T&gt; void test(T[] a, Testor&lt;T&gt; testor) { T b = a[0]; if (testor.isStrictlyPositive(a[0])) { a[0] = testor.square(a[0]); b = testor.add(b, a[1]); } } </code></pre> <p>Note for things like <code>Short</code>, summing two of them will give you an <code>int</code>.</p> <p>(Usual Stack Overflow disclaimer: not so much as attempted to compile the code.)</p>
    singulars
    1. This table or related slice is empty.
    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