[1] | 1 | package de.ugoe.cs.util;
|
---|
| 2 |
|
---|
[175] | 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 | */
|
---|
[1] | 11 | final public class ArrayTools {
|
---|
[175] | 12 |
|
---|
[1] | 13 | /**
|
---|
| 14 | * <p>
|
---|
[330] | 15 | * Private constructor to prevent initializing of the class.
|
---|
| 16 | * </p>
|
---|
| 17 | */
|
---|
| 18 | private ArrayTools() {
|
---|
| 19 |
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * <p>
|
---|
[175] | 24 | * Finds the first occurrence of an object inside an array.
|
---|
[1] | 25 | * </p>
|
---|
| 26 | * <p>
|
---|
[175] | 27 | * In case {@code other==null}, the first occurrence of a {@code null} value
|
---|
| 28 | * in the array is returned.
|
---|
[1] | 29 | * </p>
|
---|
[175] | 30 | *
|
---|
| 31 | * @param array
|
---|
| 32 | * the array
|
---|
| 33 | * @param other
|
---|
| 34 | * the object
|
---|
[1] | 35 | * @return index of the object if found, -1 otherwise
|
---|
| 36 | */
|
---|
| 37 | public static int findIndex(Object[] array, Object other) {
|
---|
| 38 | int retVal = -1;
|
---|
[175] | 39 | for (int i = 0; i < array.length && retVal == -1; i++) {
|
---|
| 40 | if (other != null) {
|
---|
| 41 | if (array[i] != null && array[i].equals(other)) {
|
---|
[1] | 42 | retVal = i;
|
---|
| 43 | }
|
---|
| 44 | } else {
|
---|
[175] | 45 | if (array[i] == null) {
|
---|
[1] | 46 | retVal = i;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | return retVal;
|
---|
| 51 | }
|
---|
[290] | 52 |
|
---|
| 53 | /**
|
---|
| 54 | * <p>
|
---|
| 55 | * Finds the highest element in an array. If multiple elements have the
|
---|
| 56 | * maximum value, the index of the first one is returned; null-values are
|
---|
| 57 | * ignored. In case the parameter array is null, has length 0 or contains
|
---|
| 58 | * only null-values, -1 is returned.
|
---|
| 59 | * </p>
|
---|
| 60 | *
|
---|
| 61 | * @param <T>
|
---|
| 62 | * @param array
|
---|
| 63 | * the array
|
---|
| 64 | * @return index of the element with the highest value, -1 in case of an
|
---|
| 65 | * invalid parameter
|
---|
| 66 | */
|
---|
| 67 | @SuppressWarnings("unchecked")
|
---|
| 68 | public static <T> int findMax(Comparable<T>[] array) {
|
---|
| 69 | int maxIndex = -1;
|
---|
| 70 | T maxElement = null;
|
---|
| 71 | if (array != null) {
|
---|
| 72 | for (int i = 0; i < array.length; i++) {
|
---|
| 73 | if (array[i] != null) {
|
---|
| 74 | if (maxElement == null
|
---|
| 75 | || array[i].compareTo(maxElement) > 0) {
|
---|
| 76 | maxElement = (T) array[i];
|
---|
| 77 | maxIndex = i;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | return maxIndex;
|
---|
| 83 | }
|
---|
[330] | 84 |
|
---|
[290] | 85 | /**
|
---|
| 86 | * <p>
|
---|
| 87 | * Finds the lowest element in an array. If multiple elements have the
|
---|
| 88 | * minimal value, the index of the first one is returned; null-values are
|
---|
| 89 | * ignored. In case the parameter array is null, has length 0 or contains
|
---|
| 90 | * only null-values, -1 is returned.
|
---|
| 91 | * </p>
|
---|
| 92 | *
|
---|
| 93 | * @param <T>
|
---|
| 94 | * @param array
|
---|
| 95 | * the array
|
---|
| 96 | * @return index of the element with the lowest value, -1 in case of an
|
---|
| 97 | * invalid parameter
|
---|
| 98 | */
|
---|
| 99 | @SuppressWarnings("unchecked")
|
---|
| 100 | public static <T> int findMin(Comparable<T>[] array) {
|
---|
| 101 | int maxIndex = -1;
|
---|
| 102 | T maxElement = null;
|
---|
| 103 | if (array != null) {
|
---|
| 104 | for (int i = 0; i < array.length; i++) {
|
---|
| 105 | if (array[i] != null) {
|
---|
| 106 | if (maxElement == null
|
---|
| 107 | || array[i].compareTo(maxElement) < 0) {
|
---|
| 108 | maxElement = (T) array[i];
|
---|
| 109 | maxIndex = i;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | return maxIndex;
|
---|
| 115 | }
|
---|
[1] | 116 | }
|
---|