I've been trying a couple of things without success
package com.codegym.task.task37.task3701;
import java.util.*;
/*
Circular iterator
*/
public class Solution<T> extends ArrayList<T> {
public static void main(String[] args) {
Solution<Integer> list = new Solution<>();
list.add(1);
list.add(2);
list.add(3);
int count = 0;
for (Integer i : list) {
//1 2 3 1 2 3 1 2 3 1
System.out.print(i + " ");
count++;
if (count == 10) {
break;
}
}
}
public Iterator<T> iterator() {
return new CircularIterator();
}
public class CircularIterator implements Iterator<T> {
int size = Solution.this.size();
int currentPointer;
int lastRet = -1;
int expectedModCount = modCount;
public CircularIterator(){
currentPointer = 0;
}
public boolean hasNext() {
return (true);
}
public T next() {
checkForComodification();
if (currentPointer >= size) {
lastRet = currentPointer;
currentPointer = 0;
}
else{
lastRet = currentPointer;
}
return Solution.this.get(currentPointer++);
}
public void remove(){
checkForComodification();
if (lastRet < 0) {
throw new IllegalStateException();
}
Solution.this.remove(lastRet);
if (lastRet < size()-1) currentPointer = lastRet;
lastRet = -1;
expectedModCount = modCount;
}
final void checkForComodification() {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
}
}
}