📄 arrayutils.java
字号:
}
return (int[]) array.clone();
}
/**
* <p>Clones an array returning a typecast result and handling
* <code>null</code>.</p>
*
* <p>This method returns <code>null</code> if <code>null</code> array input.</p>
*
* @param array the array to clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static short[] clone(final short[] array) {
if (array == null) {
return null;
}
return (short[]) array.clone();
}
/**
* <p>Clones an array returning a typecast result and handling
* <code>null</code>.</p>
*
* <p>This method returns <code>null</code> if <code>null</code> array input.</p>
*
* @param array the array to clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static char[] clone(final char[] array) {
if (array == null) {
return null;
}
return (char[]) array.clone();
}
/**
* <p>Clones an array returning a typecast result and handling
* <code>null</code>.</p>
*
* <p>This method returns <code>null</code> if <code>null</code> array input.</p>
*
* @param array the array to clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static byte[] clone(final byte[] array) {
if (array == null) {
return null;
}
return (byte[]) array.clone();
}
/**
* <p>Clones an array returning a typecast result and handling
* <code>null</code>.</p>
*
* <p>This method returns <code>null</code> if <code>null</code> array input.</p>
*
* @param array the array to clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static double[] clone(final double[] array) {
if (array == null) {
return null;
}
return (double[]) array.clone();
}
/**
* <p>Clones an array returning a typecast result and handling
* <code>null</code>.</p>
*
* <p>This method returns <code>null</code> if <code>null</code> array input.</p>
*
* @param array the array to clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static float[] clone(final float[] array) {
if (array == null) {
return null;
}
return (float[]) array.clone();
}
/**
* <p>Clones an array returning a typecast result and handling
* <code>null</code>.</p>
*
* <p>This method returns <code>null</code> if <code>null</code> array input.</p>
*
* @param array the array to clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static boolean[] clone(final boolean[] array) {
if (array == null) {
return null;
}
return (boolean[]) array.clone();
}
// Is same length
//-----------------------------------------------------------------------
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final Object[] array1, final Object[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final long[] array1, final long[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final int[] array1, final int[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final short[] array1, final short[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final char[] array1, final char[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final byte[] array1, final byte[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final double[] array1, final double[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final float[] array1, final float[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(final boolean[] array1, final boolean[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* <p>Checks whether two arrays are the same type taking into account
* multi-dimensional arrays.</p>
*
* @param array1 the first array, must not be <code>null</code>
* @param array2 the second array, must not be <code>null</code>
* @return <code>true</code> if type of arrays matches
* @throws IllegalArgumentException if either array is <code>null</code>
*/
public static boolean isSameType(final Object array1, final Object array2) {
if (array1 == null || array2 == null) {
throw new IllegalArgumentException("The Array must not be null");
}
return array1.getClass().getName().equals(array2.getClass().getName());
}
// Reverse
//-----------------------------------------------------------------------
/**
* <p>Reverses the order of the given array.</p>
*
* <p>There is no special handling for multi-dimensional arrays.</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 Object[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
Object 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 long[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
long 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>
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -