java - How to store objects in an array -
i @ stage have gone beyond syntax errors finding hard regarding why @ times code not behave correctly , 1 of times , love guidance on this.
code snippet follows:
@override public void run() { (int = 0; < eventstatus.length; i++) eventstatus[i] = new eventstatus(); random randomgenerator = new random(); boolean proceed = false; boolean finished = false; while (!finished) { //system.out.println(eventimpl.geteventtype()); if (("initial".equals(eventimpl.geteventtype())) && (!eventstatus[0].ishasoccured())) { eventstatus[0].sethasoccured(true); eventstatus[0].sethassent(true); eventstatus[0].seteventimpl(eventimpl); system.out.println("one"); consumeevent(eventstatus[0].geteventimpl()); if (eventstatus[1].ishasoccured()) { system.out.println("two"); eventstatus[1].sethassent(true); consumeevent(eventstatus[1].geteventimpl()); proceed = true; } else { proceed = false; } if ((eventstatus[2].ishasoccured()) && proceed) { system.out.println("three"); eventstatus[2].sethassent(true); consumeevent(eventstatus[2].geteventimpl()); proceed = true; } else { proceed = false; } if ((eventstatus[3].ishasoccured()) && proceed) { system.out.println("four"); eventstatus[3].sethassent(true); consumeevent(eventstatus[3].geteventimpl()); proceed = true; } else { proceed = false; } if ((eventstatus[4].ishasoccured()) && proceed) { system.out.println("five"); eventstatus[4].sethassent(true); consumeevent(eventstatus[4].geteventimpl()); proceed = true; } else { proceed = false; } system.out.println("initial here"); } if (("creating".equals(eventimpl.geteventtype())) && (!eventstatus[1].ishasoccured())) { eventstatus[1].sethasoccured(true); eventstatus[1].seteventimpl(eventimpl); system.out.println("creating here"); } if (("created".equals(eventimpl.geteventtype())) && (!eventstatus[2].ishasoccured())) { eventstatus[2].sethasoccured(true); eventstatus[2].seteventimpl(eventimpl); system.out.println("created here"); } if (("closing".equals(eventimpl.geteventtype())) && (!eventstatus[3].ishasoccured())) { eventstatus[3].sethasoccured(true); eventstatus[3].seteventimpl(eventimpl); system.out.println("closing here"); } if (("closed".equals(eventimpl.geteventtype())) && (!eventstatus[4].ishasoccured())) { eventstatus[4].sethasoccured(true); eventstatus[4].seteventimpl(eventimpl); system.out.println("closed here"); } if (!finished) { eventimpl.seteventtype(eventimpl.eventtypes.values()[randomgenerator.nextint(5)]); } } }
and helper class:
public class eventstatus { private boolean hasoccured = false; private boolean hassent = false; private eventimpl eventimpl; public boolean ishasoccured() { homecoming hasoccured; } public boolean ishassent() { homecoming hassent; } public void sethasoccured(boolean hasoccured) { this.hasoccured = hasoccured; } public void sethassent(boolean hassent) { this.hassent = hassent; } public eventimpl geteventimpl() { homecoming eventimpl; } public void seteventimpl(eventimpl eventimpl) { this.eventimpl = eventimpl; } }
the problem beingness while trying store instance of eventimpl class each 'if's when run code stored objects not run instead default 'if' object gets executed each status met.
for example:
class="lang-none prettyprint-override">closed here created here creating here closing here 1 event id: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 event: initial 2 event id: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 event: initial 3 event id: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 event: initial 4 event id: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 event: initial 5 event id: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 event: initial initial here
each of these initial except 1 should stating object stored in helper class.
so please tell me how done please?
@override public synchronized void consumeevent (event theevent) { system.out.println("event id: " + theevent.geteventid() + " event: " + theevent.geteventtype()); }
you have 1 event
(eventimpl
) object involved -- whatever stored in eventimpl
instance variable of class containing methods. @ each iteration of loop, assign reference object property of 1 of elements of eventstatus
array, , set same object's eventtype
property random value, neither action creates new object.
after set type "initial", programme goes through , consumes eventimpl
stored in each of 1 or more elements of eventstatus
array, every element contains 1 contains the same one.
it's hard me tell trying here, or why approaching do, program's reported behavior code leads me expect.
java arrays object
No comments:
Post a Comment