Changeset 290 for trunk/JavaHelperLib/src/de/ugoe/cs/util
- Timestamp:
- 12/09/11 15:55:33 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaHelperLib/src/de/ugoe/cs/util/ArrayTools.java
r175 r290 41 41 return retVal; 42 42 } 43 44 /** 45 * <p> 46 * Finds the highest element in an array. If multiple elements have the 47 * maximum value, the index of the first one is returned; null-values are 48 * ignored. In case the parameter array is null, has length 0 or contains 49 * only null-values, -1 is returned. 50 * </p> 51 * 52 * @param <T> 53 * @param array 54 * the array 55 * @return index of the element with the highest value, -1 in case of an 56 * invalid parameter 57 */ 58 @SuppressWarnings("unchecked") 59 public static <T> int findMax(Comparable<T>[] array) { 60 int maxIndex = -1; 61 T maxElement = null; 62 if (array != null) { 63 for (int i = 0; i < array.length; i++) { 64 if (array[i] != null) { 65 if (maxElement == null 66 || array[i].compareTo(maxElement) > 0) { 67 maxElement = (T) array[i]; 68 maxIndex = i; 69 } 70 } 71 } 72 } 73 return maxIndex; 74 } 75 76 /** 77 * <p> 78 * Finds the lowest element in an array. If multiple elements have the 79 * minimal value, the index of the first one is returned; null-values are 80 * ignored. In case the parameter array is null, has length 0 or contains 81 * only null-values, -1 is returned. 82 * </p> 83 * 84 * @param <T> 85 * @param array 86 * the array 87 * @return index of the element with the lowest value, -1 in case of an 88 * invalid parameter 89 */ 90 @SuppressWarnings("unchecked") 91 public static <T> int findMin(Comparable<T>[] array) { 92 int maxIndex = -1; 93 T maxElement = null; 94 if (array != null) { 95 for (int i = 0; i < array.length; i++) { 96 if (array[i] != null) { 97 if (maxElement == null 98 || array[i].compareTo(maxElement) < 0) { 99 maxElement = (T) array[i]; 100 maxIndex = i; 101 } 102 } 103 } 104 } 105 return maxIndex; 106 } 43 107 }
Note: See TracChangeset
for help on using the changeset viewer.