java - How to acces previous element in an ArrayList using "this" -
i access previous element reserved name "this" :
paper.lines.get(this-1);
is there way simply?
if this
element in list , want elements located before it, need :
paper.lines.get(paper.lines.indexof(this) - 1);
of course code lacks validations, since this
may not found in list, or may first element of list. in both cases you'll exception.
so, safer code be:
int index = paper.lines.indexof(this); if (index > 0) { return paper.lines.get(index - 1); }
Comments
Post a Comment