mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Type safe version of removeNulls.
This commit is contained in:
parent
68a906b0a6
commit
3b408710f4
1 changed files with 74 additions and 44 deletions
|
@ -69,7 +69,6 @@ public abstract class ArrayUtil {
|
|||
return haveNull ? right + 1 : -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Assumes that array contains nulls at the end, only.
|
||||
* Appends object using the current length of the array.
|
||||
|
@ -293,7 +292,7 @@ public abstract class ArrayUtil {
|
|||
* @return true if the specified array contains the specified object, or the specified array is null
|
||||
*/
|
||||
public static boolean contains(Object[] array, Object obj) {
|
||||
return indexOf(array, obj)!=-1;
|
||||
return indexOf(array, obj) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -347,16 +346,14 @@ public abstract class ArrayUtil {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Note that this should only be used when the placement of
|
||||
* nulls within the array is unknown (due to performance efficiency).
|
||||
* Note that this should only be used when the placement of nulls within the array
|
||||
* is unknown (due to performance efficiency).
|
||||
*
|
||||
* Removes all of the nulls from the array and returns a new
|
||||
* array that contains all of the non-null elements.
|
||||
* Removes all of the nulls from the array and returns a new array that contains all
|
||||
* of the non-null elements.
|
||||
*
|
||||
* If there are no nulls in the original array then the original
|
||||
* array is returned.
|
||||
* If there are no nulls in the original array then the original array is returned.
|
||||
*/
|
||||
public static Object[] removeNulls(Class<?> c, Object[] array) {
|
||||
if (array == null)
|
||||
|
@ -365,7 +362,8 @@ public abstract class ArrayUtil {
|
|||
int i;
|
||||
int validEntries = 0;
|
||||
for (i = 0; i < array.length; i++) {
|
||||
if (array[i] != null) validEntries++;
|
||||
if (array[i] != null)
|
||||
validEntries++;
|
||||
}
|
||||
|
||||
if (array.length == validEntries)
|
||||
|
@ -374,7 +372,40 @@ public abstract class ArrayUtil {
|
|||
Object[] newArray = (Object[]) Array.newInstance(c, validEntries);
|
||||
int j = 0;
|
||||
for (i = 0; i < array.length; i++) {
|
||||
if (array[i] != null) newArray[j++] = array[i];
|
||||
if (array[i] != null)
|
||||
newArray[j++] = array[i];
|
||||
}
|
||||
|
||||
return newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note that this should only be used when the placement of nulls within the array
|
||||
* is unknown (due to performance efficiency).
|
||||
*
|
||||
* Removes all of the nulls from the array and returns a new array that contains all
|
||||
* of the non-null elements.
|
||||
*
|
||||
* If there are no nulls in the original array then the original array is returned.
|
||||
* @since 5.2
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T[] removeNulls(T[] array) {
|
||||
int i;
|
||||
int validEntries = 0;
|
||||
for (i = 0; i < array.length; i++) {
|
||||
if (array[i] != null)
|
||||
validEntries++;
|
||||
}
|
||||
|
||||
if (array.length == validEntries)
|
||||
return array;
|
||||
|
||||
T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), validEntries);
|
||||
int j = 0;
|
||||
for (i = 0; i < array.length; i++) {
|
||||
if (array[i] != null)
|
||||
newArray[j++] = array[i];
|
||||
}
|
||||
|
||||
return newArray;
|
||||
|
@ -407,7 +438,7 @@ public abstract class ArrayUtil {
|
|||
}
|
||||
|
||||
/**
|
||||
* Insert the obj at the beginning of the array, shifting the whole thing one index
|
||||
* Inserts the obj at the beginning of the array, shifting the whole thing one index
|
||||
* Assumes that array contains nulls at the end, only.
|
||||
*/
|
||||
public static Object[] prepend(Class<?> c, Object[] array, Object obj) {
|
||||
|
@ -489,7 +520,6 @@ public abstract class ArrayUtil {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a new array that contains all of the elements of the
|
||||
* given array except the first one.
|
||||
|
|
Loading…
Add table
Reference in a new issue