Changeset 290


Ignore:
Timestamp:
12/09/11 15:55:33 (12 years ago)
Author:
sherbold
Message:
  • added functions findMin, findMax to find the index of the element with the minimal/maximal value in an array
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaHelperLib/src/de/ugoe/cs/util/ArrayTools.java

    r175 r290  
    4141                return retVal; 
    4242        } 
     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        } 
    43107} 
Note: See TracChangeset for help on using the changeset viewer.