I have to make a custom iterator that iterators through an array endlessly. I have no clue how to do this considering I've never worked with iterators in java before. If anyone could help me out at all and explain to me, I'd appreciate it very much.
public class Numbers{
private int[] array;
public Numbers(int[] array){
this.array = array
}
public static void main(String[] args) {
Numbers n = new Numbers();
Iterator num = n.sequence();
for(int i = 0; i < 10; i++){
if (num.hasNext()){
System.out.print(num.next() + " ");
System.out.println();
}
}
}
}
See below:
public class Numbers implements Iterable<Integer> {
private int[] array;
private int i;
public Numbers(int[] array) {
this.array = array;
i = 0;
}
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
@Override
public boolean hasNext() { return true; }
@Override
public Integer next() {
int j = i;
i = (i + 1) % array.length;
return array[j];
}
@Override
public void remove() {}
};
}
}
You could then do:
Numbers n = new Numbers(new int[]{1,2,3});
for (int i : n)
System.out.println(i); // or anything else
This would result in the infinite loop:
1
2
3
1
2
3
1
2
...
Relevant javadocs:
- Iterator
- Iterable
int[] array = new int[]{1, 2, 3}; int i = 0; while (true) { System.out.println(array[i]); // or anything else i = (i + 1) % array.length; }
This is basically how an iterator works. This example uses a List
, but you can use an iterator against any collection that implements java.lang.Iterable
.
List<String> someList; // assume this has been instantiated and has values in it
ListIterator<String> it = someList.listIterator();
while (it.hasNext()) {
String value = it.next();
// do something with value
}
Pretty much, you instantiate the iterator by telling the collection to give you a reference to its iterator. Then you loop by calling hasNext()
, which will keep you going until you have no more elements. The call to next()
pulls the next item from the iterator and increments its position by one. A call to remove()
will remove from the list the last item returned by next()
(or previous()
.)
Note, of course, that I've been using java.util.ListIterator
instead of java.util.Iterator
because the ListIterator
is a special implementation of Iterator
optimized for use against lists, like in the example I gave above.
You cannot use an iterator against an array. You'd need to use a vanilla for-loop or convert it into a List
or another object that implements Iterable
.
To loop through the above list endlessly, your loop would look something like this:
while(it.hasNext()) {
String value = it.next();
// do processing
if (!it.hasNext()) {
it = someList.listIterator(); // reset the iterator
}
}
To loop through the array using a for-loop endlessly:
for (int i = 0; i < myArray.length; i++) {
myArray[i];
// do something
if (i == myArray.length - 1) {
i = 0; // reset the index
}
}
Alteratively you could have your Numbers
class implement Iterable
directly.
Work with iterators is basically always the same.
First get the iterator from your array:
Iterator iterator = yourArray.iterator();
Second iterate while it has items:
while(iterator.hasNext()) {
Object element = iterator.next();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.