How to use Iterator/Iterable in Java? -
i want make iterator()
method in prison
class that, want make new class contain boolean hasnext()
, prisoncell next()
methods of iterator implement interface.
package iterator; import java.util.iterator; public class driver { public static void main(string args[]) { prison prison= new prison(5); iterator<prisoncell> iter; prison.addcell(new prisoncell("a", 3)); prison.addcell(new prisoncell("b", 9)); prison.addcell(new prisoncell("c", 6)); iter= prison.iterator(); //i want make iterator() method in prison class while (iter.hasnext()) system.out.println(iter.next()); } /**output here be: name: a, numprisoners: 3 name: b, numprisoners: 9 name: c, numprisoners: 6 **/ } package iterator; public class prisoncell { //how implement iterable<> iterface here? private string name; private int numprisoners; public prisoncell(string name, int numprisoners) { this.name= name; this.numprisoners= numprisoners; } public string tostring() { return "name: " + name + ", numprisoners: " + numprisoners; } } package iterator; public class prison{ prisoncell prisoncells[]; int numprisoncells; public prison(int size) { prisoncells= new prisoncell[size]; numprisoners= 0; } // nothing if array full public void addcell(prisoncell newprisoncell) { if (numprisoncells < prisoncells.length) prisoncells[numprisoncells++]= newprisoncell; } //how write iterator() method here?? } package iterator; public class iterator<prisoncell>//is supposed implement interface here? //which fields here? public iterator() //constructor here //i think boolean hasnext() , prisoncell next() methods go here?
the iterable
interface implemented collection of sort. in case, prison
class, not prisoncell
declared implement iterable<prisoncell>
.* need implement single method: iterator()
, return iterator<prisoncell>
.
what object return? 1 easy way wrap array in list
, ask list
return iterator
:
public class prison implements iterable<prisoncell> { prisoncell prisoncells[]; . . . public iterator<prisoncell> iterator() { return arrays.aslist(prisoncells).iterator(); } }
(you might consider changing prisoncells
prisoncell[]
list<prisoncell>
; recommend. ask directly return iterator
.)
alternatively, write own class implements iterator
interface methods. (this typically done private inner class of collection class, since iterator accesses internal data structures of collection—something should not exposed in public api.) should throw exception remove()
method, since operation not supported collection backed object array. (the iterator
returned list
constructed arrays.aslist()
behaves in way.)
here's quick attempt @ such inner class. wrote under assumption first numprisoncells
elements of prisoncells
array valid:
public class prison implements iterable<prisoncell> { prisoncell prisoncells[]; int numprisoncells; . . . public iterator<prisoncell> iterator() { return new prisoniterator(); } private class prisoniterator implements iterator<prisoncell> { private int index; prisoniterator() { index = -1; } public boolean hasnext() { return index < numprisoncells - 1; } public prisoncell next() { if (index < numprisoncells - 1) { return prisoncells[++index]; } else { throw new nosuchelementexception(); } } public void remove() { // implement here throw new unsupportedoperationexception(); } } }
note not robust iterator implementation; in particular, skip or repeat elements if prisoncells
array modified during iteration. java collections framework addresses having each collection object maintain internal long
modification count , when each iterator constructed, initializes own copy of modification count. @ each method call iterator, checks see collection's mod count matches internal copy and, if not, throws concurrentmodificationexception
.
*of course, might want have prisoncell
implement iterable<prisoner>
. :)
Comments
Post a Comment