Results 1 to 4 of 4
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why is this List empty ?

    It looks long, but it is very simple. Please help.

    Here is the code I have:

    Java Code:
    package myPackage2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Controller 
    {
    	private List<Event> eventList = new ArrayList<Event>();
    	
    	public void addEvent(Event c)
    	{
    		eventList.add(c);
    	}
    	
    	public void run()
    	{
    		System.out.println("Run!");
    		System.out.println(eventList.size());
    
    		while(eventList.size()>0)
    		{
    			for(Event e: new ArrayList<Event>(eventList))
    				if(e.ready())
    				{
    					System.out.println(e);
    					e.action();
    					eventList.remove(e);
    				}
    		}
    	}
    
    }
    Java Code:
    package myPackage2;
    
    public abstract class Event 
    {
    	private long eventTime;
    	protected final long delayTime;
    	
    	public Event(long delayTime)
    	{
    		this.delayTime = delayTime;
    		start();
    	}
    	
    	public void start()
    	{
    		eventTime = System.nanoTime() + delayTime;
    	}
    	
    	public boolean ready()
    	{
    		return System.nanoTime() >= eventTime;
    	}
    	
    	public abstract void action();
    
    }
    Java Code:
    package myPackage2;
    
    public class GreenHouseControls extends Controller 
    {
    	private boolean light = false;
    	
    	public class LightOn extends Event
    	{
    		public LightOn(long delayTime)
    		{
    			super(delayTime);
    		}
    		
    		public void action()
    		{
    			light = true;
    			System.out.println("Action!");
    		}
    		
    		public String toString()
    		{
    			return "Light is on";
    		}
    	}
    	
    	public class LightOff extends Event
    	{
    		public LightOff(long delayTime)
    		{
    			super(delayTime);
    		}
    		public void action()
    		{
    			light = false;
    		}
    		
    		public String toString()
    		{
    			return "Light is off";
    		}
    		
    	}
    }
    Java Code:
    package myPackage2;
    
    public class GreenHouseController 
    {
    	public static void main(String[] args)
    	{
    		GreenHouseControls gc = new GreenHouseControls();
    		
    		Event[] eventList = {gc.new LightOn(5),gc.new LightOff(15)};
    		gc.run();		
    	}
    	
    }
    So my
    Java Code:
    while(eventList.size()>0)
    is not working, because eventList.size equals zero. But why ? I added 2 elements to it ?

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why is this List empty ?

    Btw this is the output:


    Run!
    0

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Why is this List empty ?

    Where do you add anything the eventList?
    Add a println to the addEvent() to print out what is added to the list to see.

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

Similar Threads

  1. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  2. Empty JFrame
    By Wednesday Bass in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2011, 07:30 PM
  3. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  4. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  5. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •