Note that there are some explanatory texts on larger screens.

plurals
  1. POJUnit test case failure. java.lang.AssertionError: expected:<[I@12c5431> but was:<[I@14b6bed>
    primarykey
    data
    text
    <p>I have been facing this problem for a while and it starts frustrating me. The code needs to return the k elements of a nearest to val. This method will throw an IllegalArgumentException if k is negative and return an array of zero length if k == 0 or if k > a.length. When I run the test case against this method, it reports:</p> <pre><code>There was 1 failure: 1) nearestKTest(SelectorTest) java.lang.AssertionError: expected:&lt;[I@12c5431&gt; but was:&lt;[I@14b6bed&gt; at SelectorTest.nearestKTest(SelectorTest.java:21) FAILURES!!! Tests run: 1, Failures: 1 </code></pre> <p>I know this means expected didn't match actual. I just could not figure it out. :(</p> <pre><code>public static int[] nearestK(int[] a, int val, int k) { int[] b = new int[10]; for (int i = 0; i &lt; b.length; i++){ b[i] = Math.abs(a[i] - val); } Arrays.sort(b); int[] c = new int [k]; for (int i = 0; i &lt; k; i++){ if (k &lt; 0){ throw new IllegalArgumentException("k is not invalid!"); } else if (k == 0 || k &gt; a.length){ return new int[0];} else{ c[i] = b[i];} } return c; } Test case: import org.junit.Assert; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class SelectorTest { /** Fixture initialization (common initialization * for all tests). **/ @Before public void setUp() { } /** A test that always fails. **/ @Test public void nearestKTest() { int[] a = {2, 4, 6, 7, 8, 10, 13, 14, 15, 32}; int[] expected = {6, 7}; int[] actual = Selector.nearestK(a, 6, 2); Assert.assertEquals(expected,actual); } } </code></pre>
    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. 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