Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement iterator as an attribute of a class in Java
    text
    copied!<p>let's say I have this simple MyArray class, with two simple methods: add, delete and an iterator. In the main method we can see how it is supposed to be used:</p> <pre><code>public class MyArray { int start; int end; int[] arr; myIterator it; public MyArray(){ this.start=0; this.end=0; this.arr=new int[500]; it=new myIterator(); } public void add(int el){ this.arr[this.end]=el; this.end++; } public void delete(){ this.arr[this.start]=0; this.start++; } public static void main(String[] args){ MyArray m=new MyArray(); m.add(3); m.add(299); m.add(19); m.add(27); while(m.it.hasNext()){ System.out.println(m.it.next()); } } </code></pre> <p>And then MyIterator should be implemented somehow:</p> <pre><code>import java.util.Iterator; public class myIterator implements Iterator{ @Override public boolean hasNext() { // TODO Auto-generated method stub return false; } @Override public Object next() { // TODO Auto-generated method stub return null; } @Override public void remove() { // TODO Auto-generated method stub } </code></pre> <p>}</p> <p>MyIterator should iterate <em>arr</em> from <em>MyArray</em> class, from <em>start</em> to <em>end</em> values; both are also attributes of <em>MyArray</em>. So, as <em>MyIterator</em> should use <em>MyArray</em> attributes, how should MyIterator be implemented? Perhaps I can send the current object in the initialization:</p> <pre><code>it=new myIterator(this); </code></pre> <p>But I guess it's not the best soultion. Or maybe MyArray itself should implement Iterator interface? How is this solved?</p> <p>EDIT:</p> <p>Ok, thanks to everybody. This was a simple example of what I wnat to do, so don't care about fixed length array. Waht I really want to do is a circular FIFO, that's why <code>start</code> and <code>end</code> are the cursors.</p> <p>This circular FIFO will be an array of pairs of ints with, e.g., size 300: <code>int[][] arr=new int[300][2]</code>.</p> <p>When iterating a circular array I have to take care if the counter arrives to the end and make it start from the beginning, so this is how I have solved it:</p> <pre><code>if (this.start &gt;= this.end ) temp_end=this.end+this.buff.length; else temp_end=this.end; int ii; int j=0; int[] value=new int[2]; for(int i=this.start; i&lt;temp_end; i++){ ii=i% this.arr.length; value=this.buff[ii]; //do anything with value </code></pre> <p>}</p> <p>But I would like to avoid worrying about these things and just iterate in a simple way, I can do this with iterator interface, but then I have 2 problems: the first one I already explained and has been solved by many answers, and the second one is that my array is made of pairs of ints, and I can't use iterator with primitive types. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload