📄 arrayutils.java
字号:
return -1;
}
/**
* <p>Checks if the value is in the given array.</p>
*
* <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
*
* @param array the array to search through
* @param valueToFind the value to find
* @return <code>true</code> if the array contains the object
*/
public static boolean contains(final byte[] array, final byte valueToFind) {
return (indexOf(array, valueToFind) != -1);
}
// double IndexOf
//-----------------------------------------------------------------------
/**
* <p>Find the index of the given value in the array.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @return the index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final double[] array, final double valueToFind) {
return indexOf(array, valueToFind, 0);
}
/**
* <p>Find the index of the given value within a given tolerance in the array.
* This method will return the index of the first value which falls between the region
* defined by valueToFind - tolerance and valueToFind + tolerance.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param tolerance tolerance of the search
* @return the index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final double[] array, final double valueToFind, final double tolerance) {
return indexOf(array, valueToFind, 0, tolerance);
}
/**
* <p>Find the index of the given value in the array starting at the given index.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* <p>A negative startIndex is treated as zero. A startIndex larger than the array
* length will return -1.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param startIndex the index to start searching at
* @return the index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final double[] array, final double valueToFind, int startIndex) {
if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
startIndex = 0;
}
for (int i = startIndex; i < array.length; i++) {
if (valueToFind == array[i]) {
return i;
}
}
return -1;
}
/**
* <p>Find the index of the given value in the array starting at the given index.
* This method will return the index of the first value which falls between the region
* defined by valueToFind - tolerance and valueToFind + tolerance.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* <p>A negative startIndex is treated as zero. A startIndex larger than the array
* length will return -1.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param startIndex the index to start searching at
* @param tolerance tolerance of the search
* @return the index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final double[] array, final double valueToFind, int startIndex, double tolerance) {
if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
startIndex = 0;
}
double min = valueToFind - tolerance;
double max = valueToFind + tolerance;
for (int i = startIndex; i < array.length; i++) {
if (array[i] >= min && array[i] <= max) {
return i;
}
}
return -1;
}
/**
* <p>Find the last index of the given value within the array.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* @param array the array to travers backwords looking for the object, may be <code>null</code>
* @param valueToFind the object to find
* @return the last index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final double[] array, final double valueToFind) {
return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
/**
* <p>Find the last index of the given value within a given tolerance in the array.
* This method will return the index of the last value which falls between the region
* defined by valueToFind - tolerance and valueToFind + tolerance.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param tolerance tolerance of the search
* @return the index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final double[] array, final double valueToFind, final double tolerance) {
return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance);
}
/**
* <p>Find the last index of the given value in the array starting at the given index.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* <p>A negative startIndex will return -1. A startIndex larger than the array
* length will search from the end of the array.</p>
*
* @param array the array to traverse for looking for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param startIndex the start index to travers backwards from
* @return the last index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex) {
if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
return -1;
} else if (startIndex >= array.length) {
startIndex = array.length - 1;
}
for (int i = startIndex; i >= 0; i--) {
if (valueToFind == array[i]) {
return i;
}
}
return -1;
}
/**
* <p>Find the last index of the given value in the array starting at the given index.
* This method will return the index of the last value which falls between the region
* defined by valueToFind - tolerance and valueToFind + tolerance.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* <p>A negative startIndex will return -1. A startIndex larger than the array
* length will search from the end of the array.</p>
*
* @param array the array to traverse for looking for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param startIndex the start index to travers backwards from
* @param tolerance search for value within plus/minus this amount
* @return the last index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex, double tolerance) {
if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
return -1;
} else if (startIndex >= array.length) {
startIndex = array.length - 1;
}
double min = valueToFind - tolerance;
double max = valueToFind + tolerance;
for (int i = startIndex; i >= 0; i--) {
if (array[i] >= min && array[i] <= max) {
return i;
}
}
return -1;
}
/**
* <p>Checks if the value is in the given array.</p>
*
* <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
*
* @param array the array to search through
* @param valueToFind the value to find
* @return <code>true</code> if the array contains the object
*/
public static boolean contains(final double[] array, final double valueToFind) {
return (indexOf(array, valueToFind) != -1);
}
/**
* <p>Checks if a value falling within the given tolerance is in the
* given array. If the array contains a value within the inclusive range
* defined by (value - tolerance) to (value + tolerance).</p>
*
* <p>The method returns <code>false</code> if a <code>null</code> array
* is passed in.</p>
*
* @param array the array to search
* @param valueToFind the value to find
* @param tolerance the array contains the tolerance of the search
* @return true if value falling within tolerance is in array
*/
public static boolean contains(final double[] array, final double valueToFind, final double tolerance) {
return (indexOf(array, valueToFind, 0, tolerance) != -1);
}
// float IndexOf
//-----------------------------------------------------------------------
/**
* <p>Find the index of the given value in the array.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @return the index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final float[] array, final float valueToFind) {
return indexOf(array, valueToFind, 0);
}
/**
* <p>Find the index of the given value in the array starting at the given index.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* <p>A negative startIndex is treated as zero. A startIndex larger than the array
* length will return -1.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param startIndex the index to start searching at
* @return the index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final float[] array, final float valueToFind, int startIndex) {
if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
startIndex = 0;
}
for (int i = startIndex; i < array.length; i++) {
if (valueToFind == array[i]) {
return i;
}
}
return -1;
}
/**
* <p>Find the last index of the given value within the array.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* @param array the array to travers backwords looking for the object, may be <code>null</code>
* @param valueToFind the object to find
* @return the last index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final float[] array, final float valueToFind) {
return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
/**
* <p>Find the last index of the given value in the array starting at the given index.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* <p>A negative startIndex will return -1. A startIndex larger than the array
* length will search from the end of the array.</p>
*
* @param array the array to traverse for looking for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param startIndex the start index to travers backwards from
* @return the last index of the value within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final float[] array, final float valueToFind, int startIndex) {
if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
return -1;
} else if (startIndex >= array.length) {
startIndex = array.length - 1;
}
for (int i = startIndex; i >= 0; i--) {
if (valueToFind == array[i]) {
return i;
}
}
return -1;
}
/**
* <p>Checks if the value is in the given array.</p>
*
* <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
*
* @param array the array to se
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -