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

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -