简体   繁体   中英

How to store objects in an array

I am at a stage where I have gone beyond syntax errors so I am finding it difficult regarding why at times my code does not behave correctly and this is one of those times and would love your guidance on this.

Code snippet follows:

@Override
public void run() {

    for (int i = 0; i < 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 the helper class:

public class EventStatus {

    private boolean hasOccured = false;
    private boolean hasSent = false;
    private EventImpl eventImpl;

    public boolean isHasOccured() {
        return hasOccured;
    }

    public boolean isHasSent() {
        return hasSent;
    }

    public void setHasOccured(boolean hasOccured) {
        this.hasOccured = hasOccured;
    }

    public void setHasSent(boolean hasSent) {
        this.hasSent = hasSent;
    }

    public EventImpl getEventImpl() {
        return eventImpl;
    }

    public void setEventImpl(EventImpl eventImpl) {
        this.eventImpl = eventImpl;
    }

}

The problem being while I am trying to store an instance of EventImpl class for each 'If's when I run the code the stored objects do not run but instead the default for the 'if' object gets executed for each condition met.

For example:

CLOSED Here
CREATED Here
CREATING Here
CLOSING Here
one
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
two
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
three
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
four
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
five
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
INITIAL Here

Each of these INITIAL except for one should really be stating the object previously stored in my helper class.

So could you please tell me how do I get this done please?

@Override
public synchronized void consumeEvent (Event theEvent) {

    System.out.println("Event ID: " + theEvent.getEventId() + " Event: " + theEvent.getEventType());
}

You only have one Event ( EventImpl ) object involved -- whatever is stored in the eventImpl instance variable of the class containing those methods. At each iteration of the loop, you assign a reference to that object to a property of one of the elements of your eventStatus array, and you set that same object's eventType property to a random value, but neither action creates a new object.

After you eventually set the type to "INITIAL", the program goes through and consumes the EventImpl stored in each of one or more elements of your eventStatus array, but every element that contains one contains the same one.

It's hard for me to tell what you are actually trying to do here, or why you are approaching it as you do, but the program's reported behavior is exactly as its code leads me to expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM