Iteration in java

I have to make a custom iterator so that iterators through the array are infinite. I have no idea how to do this, given that I have never worked with iterators in java before. If anyone could help me and explain to me, I would really appreciate it.

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(); } } } } 
+4
source share
3 answers

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() {} }; } } 

Then you can:

 Numbers n = new Numbers(new int[]{1,2,3}); for (int i : n) System.out.println(i); // or anything else 

This will lead to an infinite loop:

1
2
3
1
2
3
1
2
...

Relevant javadocs:
- Iterator
- iterable


Another way to do this is to simply have an infinite while loop as such:
 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; } 
+5
source

This is basically how the iterator works. This example uses List , but you can use an iterator for 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 } 

To a large extent, you create an iterator instance by telling the collection to give you a link to its iterator. Then you loop through by typing hasNext() , which will keep you moving until you have more elements. Calling next() calls the next element from the iterator and increments its position by one. Calling remove() will remove the last element returned by next() (or previous() .) From the list.

Note that I used java.util.ListIterator instead of java.util.Iterator , since ListIterator is a special Iterator implementation optimized for use against lists, as in the example above.

You cannot use an iterator for an array. You will need to use vanilla for-loop or convert it to List or another object that implements Iterable .

To cycle through the list above, your loop will look something like this:

 while(it.hasNext()) { String value = it.next(); // do processing if (!it.hasNext()) { it = someList.listIterator(); // reset the iterator } } 

To cycle through an 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 } } 

Alternatively, you can implement your Numbers class Numbers directly.

+1
source

Working with iterators is basically always the same.

First get the iterator from your array:

 Iterator iterator = yourArray.iterator(); 

The second iteration when it has elements:

 while(iterator.hasNext()) { Object element = iterator.next(); } 
0
source

Source: https://habr.com/ru/post/1432598/


All Articles