/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
private static int indexOf(long[] array, long target, int start, int end) { for (int i = start; i < end; i++) { if (array[i] == target) { return i; } } return -1; }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
public void testIndexOf() { assertEquals(-1, Longs.indexOf(EMPTY, (long) 1)); assertEquals(-1, Longs.indexOf(ARRAY1, (long) 2)); assertEquals(-1, Longs.indexOf(ARRAY234, (long) 1)); assertEquals(0, Longs.indexOf(new long[] {(long) -1}, (long) -1)); assertEquals(0, Longs.indexOf(ARRAY234, (long) 2)); assertEquals(1, Longs.indexOf(ARRAY234, (long) 3)); assertEquals(2, Longs.indexOf(ARRAY234, (long) 4)); assertEquals(1, Longs.indexOf(new long[] {(long) 2, (long) 3, (long) 2, (long) 3}, (long) 3)); }
public void testIndexOf_arrayTarget() { assertEquals(0, Longs.indexOf(EMPTY, EMPTY)); assertEquals(0, Longs.indexOf(ARRAY234, EMPTY)); assertEquals(-1, Longs.indexOf(EMPTY, ARRAY234)); assertEquals(-1, Longs.indexOf(ARRAY234, ARRAY1)); assertEquals(-1, Longs.indexOf(ARRAY1, ARRAY234)); assertEquals(0, Longs.indexOf(ARRAY1, ARRAY1)); assertEquals(0, Longs.indexOf(ARRAY234, ARRAY234)); assertEquals(0, Longs.indexOf(ARRAY234, new long[] {(long) 2, (long) 3})); assertEquals(1, Longs.indexOf(ARRAY234, new long[] {(long) 3, (long) 4})); assertEquals(1, Longs.indexOf(ARRAY234, new long[] {(long) 3})); assertEquals(2, Longs.indexOf(ARRAY234, new long[] {(long) 4})); assertEquals( 1, Longs.indexOf( new long[] {(long) 2, (long) 3, (long) 3, (long) 3, (long) 3}, new long[] {(long) 3})); assertEquals( 2, Longs.indexOf( new long[] {(long) 2, (long) 3, (long) 2, (long) 3, (long) 4, (long) 2, (long) 3}, new long[] {(long) 2, (long) 3, (long) 4})); assertEquals( 1, Longs.indexOf( new long[] {(long) 2, (long) 2, (long) 3, (long) 4, (long) 2, (long) 3, (long) 4}, new long[] {(long) 2, (long) 3, (long) 4})); assertEquals( -1, Longs.indexOf( new long[] {(long) 4, (long) 3, (long) 2}, new long[] {(long) 2, (long) 3, (long) 4}));
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in * {@code array}. * * @param array an array of {@code long} values, possibly empty * @param target a primitive {@code long} value * @return the least index {@code i} for which {@code array[i] == target}, or * {@code -1} if no such index exists. */ public static int indexOf(long[] array, long target) { return indexOf(array, target, 0, array.length); }
/** * {@inheritDoc} * * @throws NullPointerException * if the wrapped array was <code>null</code>. */ @Override public int indexOf(Object o) { // Will make the method fail if array is null. if (size() < 1) { return -1; } if (o instanceof Long) { return Longs.indexOf(array, ((Long) o).longValue()); } return -1; }
public void testIndexOf() { assertEquals(-1, Longs.indexOf(EMPTY, (long) 1)); assertEquals(-1, Longs.indexOf(ARRAY1, (long) 2)); assertEquals(-1, Longs.indexOf(ARRAY234, (long) 1)); assertEquals(0, Longs.indexOf( new long[] {(long) -1}, (long) -1)); assertEquals(0, Longs.indexOf(ARRAY234, (long) 2)); assertEquals(1, Longs.indexOf(ARRAY234, (long) 3)); assertEquals(2, Longs.indexOf(ARRAY234, (long) 4)); assertEquals(1, Longs.indexOf( new long[] { (long) 2, (long) 3, (long) 2, (long) 3 }, (long) 3)); }