public class Indexable
extends java.lang.Object
index -2 means the
next-to-last element in a list.| Constructor and Description |
|---|
Indexable(java.lang.Integer size)
Creates an instance.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.Integer |
get(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 Indexable(java.lang.Integer size)
size - the size into which the index is appliedpublic java.lang.Integer get(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 any::
size is 0 or nullgetIndex(2, -3) or getIndex(1, 2))Examples:
Indexable idx = new Indexable(null);
idx.get(null, 0); // == null
idx = new Indexable(0);
idx.get(0, 0); // == null
idx = new Indexable(4);
idx.get(0); // == 0
idx.get(1); // == 1
idx.get(3); // == 3
idx.get(4); // == null
idx.get(-1); // == 3
idx.get(-2); // == 2
idx.get(-3); // == 1
idx.get(-4); // == 0
idx.get(-5); // == null
index - the index within the size