RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ArrayIterator.java
1package com.osroyale.util;
2
3import com.google.common.collect.PeekingIterator;
4
5import java.util.ArrayList;
6import java.util.Iterator;
7
15public final class ArrayIterator<E> implements PeekingIterator<E> {
16
20 private final E[] array;
21
25 private int index;
26
30 private int lastIndex = -1;
31
36 public ArrayIterator(E[] array) {
37 this.array = array;
38 }
39
40 @Override
41 public boolean hasNext() {
42 return !(index + 1 > array.length);
43 }
44
45 @Override
46 public E peek() {
47 return array[index];
48 }
49
50 @Override
51 public E next() {
52 if(index >= array.length)
53 throw new ArrayIndexOutOfBoundsException("There are no elements left to iterate over!");
54 lastIndex = index;
55 index++;
56 return array[lastIndex];
57 }
58
59 @Override
60 public void remove() {
61 if(lastIndex == -1)
62 throw new IllegalStateException("This method can only be called once after \"next\".");
63 array[lastIndex] = null;
64 lastIndex = -1;
65 }
66}