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 |
|
---|
15 | package 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 | */
|
---|
25 | final 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 highest element in an array. If multiple elements have the
|
---|
102 | * maximum 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 array
|
---|
108 | * the array
|
---|
109 | * @return index of the element with the highest value, -1 in case of an
|
---|
110 | * invalid parameter
|
---|
111 | */
|
---|
112 | public static int findMax(double[] array) {
|
---|
113 | int maxIndex = -1;
|
---|
114 | double maxElement = -Double.MAX_VALUE;
|
---|
115 | if (array != null) {
|
---|
116 | for (int i = 0; i < array.length; i++) {
|
---|
117 | if (array[i] > maxElement) {
|
---|
118 | maxElement = array[i];
|
---|
119 | maxIndex = i;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return maxIndex;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * <p>
|
---|
128 | * Finds the lowest element in an array. If multiple elements have the
|
---|
129 | * minimal value, the index of the first one is returned; null-values are
|
---|
130 | * ignored. In case the parameter array is null, has length 0 or contains
|
---|
131 | * only null-values, -1 is returned.
|
---|
132 | * </p>
|
---|
133 | *
|
---|
134 | * @param <T>
|
---|
135 | * @param array
|
---|
136 | * the array
|
---|
137 | * @return index of the element with the lowest value, -1 in case of an
|
---|
138 | * invalid parameter
|
---|
139 | */
|
---|
140 | @SuppressWarnings("unchecked")
|
---|
141 | public static <T> int findMin(Comparable<T>[] array) {
|
---|
142 | int maxIndex = -1;
|
---|
143 | T maxElement = null;
|
---|
144 | if (array != null) {
|
---|
145 | for (int i = 0; i < array.length; i++) {
|
---|
146 | if (array[i] != null) {
|
---|
147 | if (maxElement == null
|
---|
148 | || array[i].compareTo(maxElement) < 0) {
|
---|
149 | maxElement = (T) array[i];
|
---|
150 | maxIndex = i;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | return maxIndex;
|
---|
156 | }
|
---|
157 | }
|
---|