nomnom nomnom - 1 year ago 51
Java Question

How can I access the previous/next element in an ArrayList?

I iterate through an ArrayList this way:

for (T t : list){

When I did this, I never thought I had to access the previous and next elements of this element. Now my code is huge. It costs a lot if I rewrite it with:

for (int i = 0; i < list.size(); i++){

Answer Source

I think I found the solution it is simple in fact, but I cannot delete this post already because it has answers..

I just added T t = list.get(i); in the second for-loop, all other code remains unchanged.