开发者

Iterable/Multidimensional array next method issues

Disclaimer: This is for a homework assignment.

I am currently working on an assignment where I need to implement an iterable interface in order to pass each array from a square two-dimensional array. This array is supposed to represent a grid of numbers (so I will be referring to them as such [row][col]). My problem is that I want to use the same next method to iterate through the rows and the columns. First, is this possible? Second, any suggestions/hints?

My next method currently looks like this:

public Data[] next(){
Data [] holder = new Data[ray.length];
for (int i = 0; i <ray.length; i++)
    holder[i]=ray[counter][i];
counter++;
return holder;}

EDIT: I am aware of being able to switch counter and i in ray[counter][i], but I'm not sure how to have it do both if that's possible.

ray is the multidimensional array and count is an attribute of the Iterator method I've created (It's initialized to 0 and this is the only method that changes it). I know I cannot return the "column" of ray this way, so how would I go about 开发者_开发技巧having next call columns and rows?? Thanks for any of the help. I'll be standing by if you have further questions.


My problem is that I want to use the same next method to iterate through the rows and the columns. First, is this possible?

Yes it is possible, assuming you mean what I think you mean. (The phrase "iterate through the rows and the columns" is horribly ambiguous.)

Since this is a homework exercise here are a couple of hints:

  • You need two counters not one.

  • When you get to the end of one row you need to go to the start of the next row. (Obviously!) Think about what that means if you've got two counters.

This should be enough to get you on the right track.


I want a row by row iteration and a column by column iteration.

This is also a horribly ambiguous description, but I'm going to interpret it as meaning that sometimes you want to iterate left to right and top to bottom, and other times you want to iterate top to bottom and left to right.

That is also possible:

  • One possibility is to use an extra state variable to tell the iterator which direction you are iterating; i.e. row within column, or column within row.

  • Another possibility is to implement two distinct Iterator classes for the two directions.


The problem is that the iterator class is only supposed to have one counter and returns an single-dimension array.

You've (finally) told us unambiguously that the iterator is supposed to return an array. (A good dentist could pull out a tooth quicker than that!)

So here's a hint:

  • Returning the ith row is easy, but returning the jth column requires you to create a new array to hold the values in that column.


My advice is: transform the 2d array to a list and iterate.

When initialize the Iterator, transform the list. Then you could iterate the list easily.

Following is p-code, you could enrich the implementation in your homework. Hope it helps you!

class TwoDimeIterator implements Iterator<Date> {

    List transformedList = new ArrayList();

    int cursor = 0;

    /** transform to a list row by row. 
        So you could define your Iterator order.**/
    TwoDimeIterator(){
      for(int i=0; i < ray.length; i++)
          for(int j=0; j < ray[0].length; j++)
              transformedList.add(ray[i][j]);
    }

    public Date next() {
        return transformedList.get(cursor++);
    }

    public boolean hasNext() {
        return cursor != transformedList.size();
    }

//...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜