1 | package de.ugoe.cs.util;
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * <p>
|
---|
5 | * Helper class that provides methods to simplify working with arrays.
|
---|
6 | * </p>
|
---|
7 | *
|
---|
8 | * @author Steffen Herbold
|
---|
9 | * @version 1.0
|
---|
10 | */
|
---|
11 | final public class ArrayTools {
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * <p>
|
---|
15 | * Finds the first occurrence of an object inside an array.
|
---|
16 | * </p>
|
---|
17 | * <p>
|
---|
18 | * In case {@code other==null}, the first occurrence of a {@code null} value
|
---|
19 | * in the array is returned.
|
---|
20 | * </p>
|
---|
21 | *
|
---|
22 | * @param array
|
---|
23 | * the array
|
---|
24 | * @param other
|
---|
25 | * the object
|
---|
26 | * @return index of the object if found, -1 otherwise
|
---|
27 | */
|
---|
28 | public static int findIndex(Object[] array, Object other) {
|
---|
29 | int retVal = -1;
|
---|
30 | for (int i = 0; i < array.length && retVal == -1; i++) {
|
---|
31 | if (other != null) {
|
---|
32 | if (array[i] != null && array[i].equals(other)) {
|
---|
33 | retVal = i;
|
---|
34 | }
|
---|
35 | } else {
|
---|
36 | if (array[i] == null) {
|
---|
37 | retVal = i;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 | return retVal;
|
---|
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 | }
|
---|
107 | }
|
---|