add indexOf(value) and indexOf(value, offset)

This commit is contained in:
2017-10-13 17:02:01 +02:00
parent cbe468cae8
commit 6df2553fae
2 changed files with 65 additions and 1 deletions

View File

@@ -26,7 +26,6 @@ public final class IntList implements Serializable, Cloneable {
// TODO add removeRange
// TODO add replace
// TODO add lastIndexOf
// TODO add indexOf
// TODO remove bounds checks that are handled by java, e.g. negative indices
// TODO toString
@@ -352,6 +351,39 @@ public final class IntList implements Serializable, Cloneable {
return StreamSupport.intStream(spliterator, true);
}
/**
* Returns the index of the first occurrence of {@code value}, or -1 if it does
* not exist.
*
* @param value
* the value
* @return the index, or -1
*/
public int indexOf(final int value) {
return indexOf(value, 0);
}
/**
* Returns the index of the first occurrence of {@code value} starting at the
* {@code offset}'s element, or -1 if it does not exist.
*
* @param value
* the value
* @param offset
* the offset
* @return the index, or -1
* @throws ArrayIndexOutOfBoundsException
* if offset is negative, or larger than the size of the list
*/
public int indexOf(final int value, final int offset) {
for (int i = offset; i < index; i++) {
if (data[i] == value) {
return i;
}
}
return -1;
}
@Override
public String toString() {