/** * Returns the index of the last appearance of the value {@code target} in {@code array}. Note * that this always returns {@code -1} when {@code target} is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
private static int lastIndexOf(double[] array, double target, int start, int end) { for (int i = end - 1; i >= start; i--) { if (array[i] == target) { return i; } } return -1; }
/** * Returns the index of the last appearance of the value {@code target} in {@code array}. Note * that this always returns {@code -1} when {@code target} is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
public void testLastIndexOf() { assertEquals(-1, Doubles.lastIndexOf(EMPTY, (double) 1)); assertEquals(-1, Doubles.lastIndexOf(ARRAY1, (double) 2)); assertEquals(-1, Doubles.lastIndexOf(ARRAY234, (double) 1)); assertEquals(0, Doubles.lastIndexOf(new double[] {(double) -1}, (double) -1)); assertEquals(0, Doubles.lastIndexOf(ARRAY234, (double) 2)); assertEquals(1, Doubles.lastIndexOf(ARRAY234, (double) 3)); assertEquals(2, Doubles.lastIndexOf(ARRAY234, (double) 4)); assertEquals( 3, Doubles.lastIndexOf( new double[] {(double) 2, (double) 3, (double) 2, (double) 3}, (double) 3)); for (double value : NUMBERS) { assertEquals("" + value, 0, Doubles.lastIndexOf(new double[] {value, 5.0}, value)); } assertEquals(-1, Doubles.lastIndexOf(new double[] {NaN, 5.0}, NaN)); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in {@code array}. Note * that this always returns {@code -1} when {@code target} is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in {@code array}. Note * that this always returns {@code -1} when {@code target} is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in * {@code array}. Note that this always returns {@code -1} when {@code target} * is {@code NaN}. * * @param array an array of {@code double} values, possibly empty * @param target a primitive {@code double} value * @return the greatest index {@code i} for which {@code array[i] == target}, * or {@code -1} if no such index exists. */ public static int lastIndexOf(double[] array, double target) { return lastIndexOf(array, target, 0, array.length); }
/** * {@inheritDoc} * * @throws NullPointerException * if the wrapped array was <code>null</code>. */ @Override public int lastIndexOf(Object o) { // Will make the method fail if array is null. if (size() < 1) { return -1; } if (o instanceof Double) { return Doubles.lastIndexOf(array, ((Double) o).doubleValue()); } return -1; }
public void testLastIndexOf() { assertEquals(-1, Doubles.lastIndexOf(EMPTY, (double) 1)); assertEquals(-1, Doubles.lastIndexOf(ARRAY1, (double) 2)); assertEquals(-1, Doubles.lastIndexOf(ARRAY234, (double) 1)); assertEquals(0, Doubles.lastIndexOf( new double[] {(double) -1}, (double) -1)); assertEquals(0, Doubles.lastIndexOf(ARRAY234, (double) 2)); assertEquals(1, Doubles.lastIndexOf(ARRAY234, (double) 3)); assertEquals(2, Doubles.lastIndexOf(ARRAY234, (double) 4)); assertEquals(3, Doubles.lastIndexOf( new double[] { (double) 2, (double) 3, (double) 2, (double) 3 }, (double) 3)); for (double value : NUMBERS) { assertEquals("" + value, 0, Doubles.lastIndexOf(new double[] {value, 5.0}, value)); } assertEquals(-1, Doubles.lastIndexOf(new double[] {NaN, 5.0}, NaN)); }