java - How does iterator work with constructor -
hi i'm new java , encountered following problem in homework. i'm required write class adds new object list when void method called. hint, structure of iterator method given, core structure of code looks this:
public class objectlist implements iterable<obj> { private arraylist<obj> objectlist; attribute_a a; attribute_b b; attribute_c c; public objectlist(attribute_a a, attribute_b b, attribute_c c){ objectlist = new arraylist<obj>; this.a = a; this.b = b; this.c = c; } public void extendlist(attribute_a a, attribute_b b, attribute_c c){ objectlist.add(new obj(a,b,c)); } public iterator<obj> iterator(){ return objectlist.iterator(); } @override public string tostring(){ newstr = ""; for(i = 0;i<objectlist.size();i++) { //assuming obj has method tostring() //it prints out details of each object, join 1 string newstr += objectlist.get(i).tostring(); } return newstr; } }
i'm told use java.util.iterator, not custom iterator - saved me defining hasnext(), next(), remove(), etc.
edit: goal able print objects stored in objectlist, after extendlist() called several times i.e. there many items stored in list. can print latest item in (only 1 item in list).
what have make "cursor" automatically point next item -> attribute -> perform tasks on attribute -> next item, etc. until list finished?
let's see what's going on here...
so, have class, , has field called objectlist
. when create new objectlist
instance, constructor gets called , field initialised empty list. far good. (however, why have fields called a
, b
, c
? don't seem used @ all, seems confusing require pass 3 parameters constructor ignored.)
then, whenever extendlist
called, new obj
added instance's list. looks right. means @ given point, can size of objectlist
equal number of times extendlist
has been called on object.
equally, calling iterator
return standard java iterator list. iterator should visit every obj
in list, number of items equal number of times extendlist
called on same objectlist
object.
so - why doesn't work?
it's not clear code you've posted. class looks ok, conclusion must calling wrong. guess you're constructing multiple instances of class - every time call constructor, create new instance different fields. example, won't expect:
new objectlist().extendlist("a1", "b1", "c1"); new objectlist().extendlist("a2", "b2", "c2"); new objectlist().extendlist("a3", "b3", "c3"); return new objectlist().iterator();
because new instance created each time, throwing state away before. you'd want rewrite as:
objectlist ol = new objectlist(); ol.extendlist("a1", "b1", "c1"); ol.extendlist("a2", "b2", "c2"); ol.extendlist("a3", "b3", "c3"); return ol.iterator();
if doesn't solve it, @ how you're using class, count how many times call extendlist
on same instance iterator from. if in doubt - java count - add system.out.println("in extendlist")
calls (and perhaps in constructor) see gets called when. in fact, if there's concern different instances of class being used, can unique identifier specific instance system.identityhashcode()
, e.g.:
public void extendlist(attribute_a a, attribute_b b, attribute_c c){ system.out.println("extending list " + system.identityhashcode() + " " + + ", " + b + ", " + c); objectlist.add(new obj(a,b,c)); }
failing that, may worth getting familiar how use debugger, , stepping through program line line. let see state of program @ every step, , it'll make clear things start diverging expectations.
(i'll encourage use standard java naming conventions, code surprisingly challenging read @ moment without them. class names should start capitals (and in uppercamelcase). variables , field names should start lower case letters (and in lowercamelcase). @ moment class name looks variable, , fields generic parameters. new objectlist()
looks wrong!)
Comments
Post a Comment