I have a list, and I would like to get the values ββat positions i-1 , i and i+1 . When i is the first or last index, it would throw an IndexOutOfBoundsException . To prevent this from happening, I would write some if-statements and hardcode, like this:
if (i == 0){ a = list.get(list.size()-1); b = list.get(0); c = list.get(1); } else if (i == list.size()-1){ a = list.get(i-1); b = list.get(i); c = list.get(0); } else { a = list.get(i-1); b = list.get(i); c = list.get(i+1); }
I find this way a little static. Say I want to get n entries from a list this way, how would you do it?
source share