I wanted to know if there\'s a native method in array for Java to get the index of the table for a given value ?
Let\'s say my table contains these strings :
<
Testable mockable interafce
public interface IArrayUtility {
int find(T[] list, T item);
}
implementation
public class ArrayUtility implements IArrayUtility {
@Override
public int find(T[] array, T search) {
if(array == null || array.length == 0 || search == null) {
return -1;
}
int position = 0;
for(T item : array) {
if(item.equals(search)) {
return position;
} else {
++position;
}
}
return -1;
}
}
Test
@Test
public void testArrayUtilityFindForExistentItemReturnsPosition() {
// Arrange
String search = "bus";
String[] array = {"car", search, "motorbike"};
// Act
int position = arrayUtility.find(array, search);
// Assert
Assert.assertEquals(position, 1);
}