2

Can you create a single Iterator that will step over all spaces in a 2d array?

asked Sep 10, 2011 at 6:36
5
  • 7
    What have you tried? Commented Sep 10, 2011 at 6:41
  • @Brian - that is an excellent link! Commented Sep 10, 2011 at 7:00
  • Yeah - I saw someone use it on here in this fashion, and I've now adopted it. I think it explains the issue without being a rant or too harsh. Commented Sep 10, 2011 at 7:02
  • What iteration order would you use? X,Y,X1,Y1,X2,Y2 etc.. or X,X1,X2... Y,Y1,Y2 etc...? Commented Sep 10, 2011 at 7:56
  • This is actually quite a sensible question and quite hard to do. I would not expect an inexperienced programmer to be able to do it. Commented Mar 24, 2016 at 9:05

5 Answers 5

4

If you implement the Iterable interface, you can use a for-each loop. Related examples may be found here.

answered Sep 10, 2011 at 11:54

Comments

2

Yes, wrap the array in an object and make the object implement the iterator interface. So it can be done. I am not aware of any such iterator that ships with the Jdk.

answered Sep 10, 2011 at 6:40

Comments

1

Yes, it can be done, as @Scorpion says. In fact, the solution is probably pretty simple: no more than 10 lines of executable code ... if I correctly understand the problem.

No, there isn't a convenience method in the JDK to do this. And I'm not aware of one in any of the "commons" libraries. (Reason: this particular problem is too specialized to be useful to more than a handful of programmers.)

This should be a sufficient answer for you to go and implement the solution yourself.


Should I / we provide you a potted solution? IMO, no.

  • StackOverflow is not a "we write your code for free" service.

  • If you do it yourself you will learn more: read the excellent "What have you tried?" blog article.

(And even if someone did feel like writing the code for you, you didn't give a clear enough description of the problem to implement ... without making lots of guesses.)

answered Sep 10, 2011 at 7:09

Comments

1

I dont see the need to make a single iterator when invoking the two from the arrays works just fine as per example:

int 2dArray[][];
for(int 1dArray[]: 2dArray){
 for(int i: 1dArray){
 //do stuff
 }
}
VMAtm
28.4k17 gold badges83 silver badges133 bronze badges
answered Dec 18, 2012 at 12:50

Comments

1
import java.util.LinkedList;
import java.util.Queue;
public class TwoDIterator {
int[][] array;
int outerCursor;
int lastArrayLen;
int totalElems;
int tracker = 1;
Queue<Integer> myQueue = new LinkedList<>();
public TwoDIterator(int[][] arr) {
 this.array = arr;
 this.outerCursor = 0;
 for (int i = 0; i < arr.length; i++) {
 for (int j = 0; j < arr[i].length; j++) {
 totalElems += 1;
 }
 }
 for (int i = 0; i < array[0].length; i++) {
 myQueue.add(array[0][i]);
 }
}
public boolean hasNext() {
 return array.length > outerCursor && totalElems >= tracker;
}
public Integer next() {
 if (myQueue.isEmpty()) {
 outerCursor++;
 for (int i = 0; i < array[outerCursor].length; i++) {
 myQueue.add(array[outerCursor][i]);
 }
 if (!myQueue.isEmpty()) {
 tracker++;
 return myQueue.remove();
 }
 } else {
 tracker++;
 return myQueue.remove();
 }
 return -1;
}
public static void main(String[] args) {
 int[][] arr = { { 1, 2, 3 }, { 1, 3 }, { 1, 2, 5 } };
 TwoDIterator iter = new TwoDIterator(arr);
 while (iter.hasNext()) {
 System.out.println(iter.next());
 }
}
}
answered Aug 11, 2017 at 1:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.