title: Java: easily peek backwards or forwards in a list
body: <p>I'm coding a lexical analyzer in java and need to look backwards or forwards easily in a list of custom datatypes (my tokens). I've tried saving the next and previous item as a copy, but then I figured out that I need to look arbitrarily far ahead or back. I then tried to use an index, but it was beyond unpleasant to debug that since I had to think about decreasing, increasing and getting the current position in a pinch (I even had the objects store an int of where they were at) all the while keeping within range of the list, so it was an ugly, hard to read mess of spaghetti code too at that.</p>
  
  <p>I then looked into <a href="http://www.tutorialspoint.com/java/java_linkedlist_class.htm" rel="nofollow">linked lists</a>, but they don't quite work like I want them too. I want a node and I want to be able to look ahead for two or three positions, or back, and I didn't really find any good tools for that at that place.</p>
  
  <p>Right now, I'm trying out <a href="http://www.tutorialspoint.com/java/java_using_iterator.htm" rel="nofollow">iterators</a> but I have the same problem as with indexes: I have to decrease and increase back again to where I was at since next() moves the cursor instead of just "peeking ahead".</p>
  
  <p>I'm thinking of coding my own linked list and just hitting <code>node.next().next()</code> if I want to go two steps forward, or a loop repeatedly hitting it if I want to go longer than that. Is there any built in way in Java saving me from this?</p>