Created
May 20, 2017 18:46
-
-
Save fermopili/a080297b9073b4767bbff086347ea802 to your computer and use it in GitHub Desktop.
com.javarush.task.task37.task3701
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3701; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.function.Consumer; | |
/* | |
Круговой итератор | |
*/ | |
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; | |
} | |
} | |
} | |
@Override | |
public Iterator<T> iterator() | |
{ | |
return new RoundIterator ( ); | |
} | |
public class RoundIterator implements Iterator<T> | |
{ | |
Iterator<T> iterator = Solution.super.iterator ( ); | |
int cursor = 0; | |
@Override | |
public boolean hasNext() | |
{ | |
if (size ( ) == 0) | |
return false; | |
if (cursor >= size ( )) | |
{ | |
iterator = Solution.super.iterator ( ); | |
cursor = 0; | |
} | |
return true; | |
} | |
@Override | |
public T next() | |
{ | |
cursor++; | |
return iterator.next ( ); | |
} | |
@Override | |
public void remove() | |
{ | |
iterator.remove ( ); | |
} | |
@Override | |
public void forEachRemaining(Consumer<? super T> action) | |
{ | |
iterator.forEachRemaining ( action ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment