📄 arrayutils.java
字号:
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(final int[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
int tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* <p>Reverses the order of the given array.</p>
*
* <p>This method does nothing if <code>null</code> array input.</p>
*
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(final short[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
short tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* <p>Reverses the order of the given array.</p>
*
* <p>This method does nothing if <code>null</code> array input.</p>
*
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(final char[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
char tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* <p>Reverses the order of the given array.</p>
*
* <p>This method does nothing if <code>null</code> array input.</p>
*
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(final byte[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
byte tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* <p>Reverses the order of the given array.</p>
*
* <p>This method does nothing if <code>null</code> array input.</p>
*
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(final double[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
double tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* <p>Reverses the order of the given array.</p>
*
* <p>This method does nothing if <code>null</code> array input.</p>
*
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(final float[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
float tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* <p>Reverses the order of the given array.</p>
*
* <p>This method does nothing if <code>null</code> array input.</p>
*
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(final boolean[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
boolean tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
// IndexOf search
// ----------------------------------------------------------------------
// Object IndexOf
//-----------------------------------------------------------------------
/**
* <p>Find the index of the given object 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 objectToFind the object to find, may be <code>null</code>
* @return the index of the object within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final Object[] array, final Object objectToFind) {
return indexOf(array, objectToFind, 0);
}
/**
* <p>Find the index of the given object 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 <code>-1</code>.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param objectToFind the object to find, may be <code>null</code>
* @param startIndex the index to start searching at
* @return the index of the object within the array starting at the index,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
if (array == null) {
return -1;
}
if (startIndex < 0) {
startIndex = 0;
}
if (objectToFind == null) {
for (int i = startIndex; i < array.length; i++) {
if (array[i] == null) {
return i;
}
}
} else {
for (int i = startIndex; i < array.length; i++) {
if (objectToFind.equals(array[i])) {
return i;
}
}
}
return -1;
}
/**
* <p>Find the last index of the given object 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 objectToFind the object to find, may be <code>null</code>
* @return the last index of the object within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final Object[] array, final Object objectToFind) {
return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
}
/**
* <p>Find the last index of the given object 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 <code>-1</code>. 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 objectToFind the object to find, may be <code>null</code>
* @param startIndex the start index to travers backwards from
* @return the last index of the object within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) {
if (array == null) {
return -1;
}
if (startIndex < 0) {
return -1;
} else if (startIndex >= array.length) {
startIndex = array.length - 1;
}
if (objectToFind == null) {
for (int i = startIndex; i >= 0; i--) {
if (array[i] == null) {
return i;
}
}
} else {
for (int i = startIndex; i >= 0; i--) {
if (objectToFind.equals(array[i])) {
return i;
}
}
}
return -1;
}
/**
* <p>Checks if the object 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 objectToFind the object to find
* @return <code>true</code> if the array contains the object
*/
public static boolean contains(final Object[] array, final Object objectToFind) {
return (indexOf(array, objectToFind) != -1);
}
// long 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 long[] array, final long 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 long[] array, final long valueToFind, int startIndex) {
if (array == null) {
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 long[] array, final long valueToFind) {
return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -