public class Index
extends java.lang.Object
index -2 means the next-to-last
element in a list.| Constructor and Description |
|---|
Index() |
| Modifier and Type | Method and Description |
|---|---|
static java.lang.Integer |
getIndex(java.lang.Integer size,
java.lang.Integer index)
Converts
index, which can be positive or negative, to an offset within the
given size, representing the length of a list (including strings). |
public static java.lang.Integer getIndex(java.lang.Integer size,
java.lang.Integer index)
Converts index, which can be positive or negative, to an offset within the
given size, representing the length of a list (including strings). A negative
index will result in the distance from the end of the list, with index
-1 meaning the last element in the list, -2 the second to last, etc. Returns null
if:
size is 0 or nullgetIndex(2, -3) or
getIndex(1, 2))Examples:
getIndex(null, 0); // == null
getIndex(0, 0); // == null
getIndex(4, 0); // == 0
getIndex(4, 1); // == 1
getIndex(4, 3); // == 3
getIndex(4, 4); // == null
getIndex(4, -1); // == 3
getIndex(4, -2); // == 2
getIndex(4, -3); // == 1
getIndex(4, -4); // == 0
getIndex(4, -5); // == null
size - the size into which the index is appliedindex - the index within the size