Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

[java] Monitoring changes in ArrayList

This topic is 3145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folks, Is it possible to monitor an ArrayList to determine whether an item has been added or removed, using some kind of PropertyChangeListener, Observable or some other event handler? I am using a Java graphics library and need to be able to draw new objects as they are added, which may be at arbitrary times, so I need to be able to somehow listen for new items being added to the ArrayList. Any thoughts on how to do this? I've Googled and can't really find much. TIA, ukd.

Share this post


Link to post
Share on other sites
Advertisement
ArrayList is an optimized low-level class; it doesn't implement such high-level functionality. You'd have to add the event handling yourself, which has less to do with Java than software engineering in general. Consider the decorator pattern. Or, if you don't want it to be particularly extensible, you could implement it as simply as this:


// I don't know what your list is for, so excuse the generic names
List<T> objects = new ArrayList<T>() {
@Override
public boolean add(T object) {
super.add(object); // Always returns true for ArrayList
repaint();
return true;
}

@Override
public boolean remove(T object) {
boolean result = super.remove(object);
if(result)
repaint();
return result;
}

// you get the idea...
};

Share this post


Link to post
Share on other sites
What you're after is a simple extension of ArrayList but with property change support. Unfortunately you can't extend PropertyChangeSupport as you already need to extend ArrayList, but luckily this sort of thing is a minimal amount of code.

Basically, extend ArrayList and override all methods that modify the internal array (see Collection for a better view of those). Every time the array is modified, loop over the collection of listeners listening to your new ArrayList class and tell them what's happened. A snippet:


private ArrayList<PropertyChangeListener> _listeners = new ArrayList<PropertyChangeListener>(0);

protected void firePropertyChange(String type, E oldValue, E newValue)
{
PropertyChangeEvent event = new PropertyChangeEvent(this,type,oldValue,newValue);
for (PropertyChangeListener listener : _listeners)
listener.propertyChange(event);
}



edit: if you're still unsure after reading these replies I've actually got a class that does exactly what you've enquired about at home, so I can provide the source if you want.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!