source: trunk/java-utils/src/main/java/de/ugoe/cs/util/ArrayTools.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 3.4 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.util;
16
17/**
18 * <p>
19 * Helper class that provides methods to simplify working with arrays.
20 * </p>
21 *
22 * @author Steffen Herbold
23 * @version 1.0
24 */
25final public class ArrayTools {
26
27        /**
28         * <p>
29         * Private constructor to prevent initializing of the class.
30         * </p>
31         */
32        private ArrayTools() {
33
34        }
35
36        /**
37         * <p>
38         * Finds the first occurrence of an object inside an array.
39         * </p>
40         * <p>
41         * In case {@code other==null}, the first occurrence of a {@code null} value
42         * in the array is returned.
43         * </p>
44         *
45         * @param array
46         *            the array
47         * @param other
48         *            the object
49         * @return index of the object if found, -1 otherwise
50         */
51        public static int findIndex(Object[] array, Object other) {
52                int retVal = -1;
53                for (int i = 0; i < array.length && retVal == -1; i++) {
54                        if (other != null) {
55                                if (array[i] != null && array[i].equals(other)) {
56                                        retVal = i;
57                                }
58                        } else {
59                                if (array[i] == null) {
60                                        retVal = i;
61                                }
62                        }
63                }
64                return retVal;
65        }
66
67        /**
68         * <p>
69         * Finds the highest element in an array. If multiple elements have the
70         * maximum value, the index of the first one is returned; null-values are
71         * ignored. In case the parameter array is null, has length 0 or contains
72         * only null-values, -1 is returned.
73         * </p>
74         *
75         * @param <T>
76         * @param array
77         *            the array
78         * @return index of the element with the highest value, -1 in case of an
79         *         invalid parameter
80         */
81        @SuppressWarnings("unchecked")
82        public static <T> int findMax(Comparable<T>[] array) {
83                int maxIndex = -1;
84                T maxElement = null;
85                if (array != null) {
86                        for (int i = 0; i < array.length; i++) {
87                                if (array[i] != null) {
88                                        if (maxElement == null
89                                                        || array[i].compareTo(maxElement) > 0) {
90                                                maxElement = (T) array[i];
91                                                maxIndex = i;
92                                        }
93                                }
94                        }
95                }
96                return maxIndex;
97        }
98
99        /**
100         * <p>
101         * Finds the lowest element in an array. If multiple elements have the
102         * minimal value, the index of the first one is returned; null-values are
103         * ignored. In case the parameter array is null, has length 0 or contains
104         * only null-values, -1 is returned.
105         * </p>
106         *
107         * @param <T>
108         * @param array
109         *            the array
110         * @return index of the element with the lowest value, -1 in case of an
111         *         invalid parameter
112         */
113        @SuppressWarnings("unchecked")
114        public static <T> int findMin(Comparable<T>[] array) {
115                int maxIndex = -1;
116                T maxElement = null;
117                if (array != null) {
118                        for (int i = 0; i < array.length; i++) {
119                                if (array[i] != null) {
120                                        if (maxElement == null
121                                                        || array[i].compareTo(maxElement) < 0) {
122                                                maxElement = (T) array[i];
123                                                maxIndex = i;
124                                        }
125                                }
126                        }
127                }
128                return maxIndex;
129        }
130}
Note: See TracBrowser for help on using the repository browser.