Line | |
---|
1 | package de.ugoe.cs.util;
|
---|
2 |
|
---|
3 | final public class ArrayTools {
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * <p>
|
---|
7 | * Finds the first occurence of an object inside an array.
|
---|
8 | * </p>
|
---|
9 | * <p>
|
---|
10 | * In case {@code other==null}, the first occurence of a {@code null} value in the array is returned.
|
---|
11 | * </p>
|
---|
12 | *
|
---|
13 | * @param array the array
|
---|
14 | * @param other the object
|
---|
15 | * @return index of the object if found, -1 otherwise
|
---|
16 | */
|
---|
17 | public static int findIndex(Object[] array, Object other) {
|
---|
18 | int retVal = -1;
|
---|
19 | for( int i=0 ; i<array.length && retVal==-1 ; i++ ) {
|
---|
20 | if( other!=null ) {
|
---|
21 | if( array[i]!=null && array[i].equals(other) ) {
|
---|
22 | retVal = i;
|
---|
23 | }
|
---|
24 | } else {
|
---|
25 | if( array[i]==null ) {
|
---|
26 | retVal = i;
|
---|
27 | }
|
---|
28 | }
|
---|
29 | }
|
---|
30 | return retVal;
|
---|
31 | }
|
---|
32 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.