I have a jagged array.
How can I override next()
, so I can get its elements step-by-step?
Asked
Active
Viewed 297 times
-6

Jonas
- 121,568
- 97
- 310
- 388

Svetlana Egorova
- 1
- 1
-
You could start by seeing how an iterator works internally. – BackSlash Sep 17 '18 at 12:59
-
Can you show us what you have tried? Here on Stackoverflow, ppl want to see that effort has been made and be able to help you find a solution. – Jack Flamp Sep 17 '18 at 13:03
-
5Arrays do not possess instance methods. You cannot overwrite what isn't there... – Turing85 Sep 17 '18 at 13:05
-
For example, i have an array: int it = { {1, 2}, {3, 4, 5}}. I need to override next(), to get step-by-step 1, 2, 3, 4 and 5. In the output I want to see: it.next() - i get 1, it.next() - I get 2 and etc. – Svetlana Egorova Sep 17 '18 at 13:08
-
If you want to access all elements in a nested `array` you can just as well use nested loops. – Jack Flamp Sep 17 '18 at 13:08
-
2As @Turing85 mentioned there are no iterators for arrays so it's impossible to override next(). But you can start from [here](https://stackoverflow.com/a/14248833/9949315) and modify the code for 2d jagged arrays – Maxim Sep 17 '18 at 13:11
-
No, not list, but an array, I've wrote an example above. – Svetlana Egorova Sep 17 '18 at 13:11
3 Answers
1
This might be a wrong answer to your question. I'll remove it in that case, but maybe you can use it for what you want to achieve:
int[][] it = {{1,2}, {3,4,5}};
OfInt iterator = Arrays.stream(it).flatMapToInt(x -> IntStream.of(x)).iterator();
iterator.forEachRemaining((IntConsumer) System.out::print);
Stream the jagged array, flatmap it into one single IntStream
and then do what you want with it. In this example I fetched the iterator but you might only want:
Arrays.stream(it).flatMapToInt(x -> IntStream.of(x)).forEach((IntConsumer) System.out::print);
In forEach
you can do what you need, or use some other method of IntStream

Jack Flamp
- 1,223
- 1
- 16
- 32
0
Thank you all for your answers, I've found my answer in russian stackoverflow: https://ru.stackoverflow.com/questions/867881/java-iterator-%D0%B4%D0%BB%D1%8F-%D0%BC%D0%BD%D0%BE%D0%B3%D0%BE%D0%BC%D0%B5%D1%80%D0%BD%D0%BE%D0%B3%D0%BE-%D0%BC%D0%B0%D1%81%D1%81%D0%B8%D0%B2%D0%B0
public class IteratorFor2DArray implements Iterator {
private int size;
private int i = 0;
private int j = 0;
private int[][] values = new int[i][j];
private int position = 0;
public IteratorFor2DArray(int[][] values) {
this.values = values;
this.size = countOfElements(values);
}
private int countOfElements(int[][] values) {
int count = 0;
for (int[] row : values) {
count += row.length;
}
return count;
}
@Override
public boolean hasNext() {
return position < size;
}
@Override
public Integer next() {
if (position >= size) {
throw new NoSuchElementException();
}
int element = values[i][j];
position++;
j++;
while (i < values.length && j >= values[i].length) {
j = 0;
i++;
}
return element;
}
}

Svetlana Egorova
- 1
- 1
0
I've also found another way:
public class IteratorFor2DArray implements Iterator {
private int[][] data;
private int i, j;
public IteratorFor2DArray(int[][] data) {
this.data = data;
}
@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
int element = data[i][j];
j++;
while (i < data.length && j >= data[i].length) {
j = 0;
i++;
}
return element;
}
@Override
public boolean hasNext() {
return (i < data.length && j < data[i].length);
}
}

Vega
- 27,856
- 27
- 95
- 103

Svetlana Egorova
- 1
- 1