1

I would like to access previous element from the reserved name "this" like that :

paper.lines.get(this-1);

Is there a way to do it simply?

1 Answers1

2

If this is an element in the List and you want the elements located before it, you need :

paper.lines.get(paper.lines.indexOf(this) - 1);

Of course this code lacks validations, since this may not be found in the List, or it may be the first element of the List. In both cases you'll get an exception.

So, safer code would be:

int index = paper.lines.indexOf(this);
if (index > 0) {
    return paper.lines.get(index - 1);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • I made it in another way final, indexOf(this) is a little bit tricky. when I create my object I set an id parameter... way more simple... – Nicolas Nahornyj Jun 10 '15 at 14:11