list - How to add listener on ArrayList in java -
i want create own implementation of arraylist in java, can listen when list changing , action when happens. have read, understand can't extend arraylist , add listener.
i want use mylist in class variable public modifier, users can change directly , done action when changes it.
class mylist extends arraylist<object>.... { ... } class useofmylist { public mylist places = new mylist<object>(); places.add("buenos aires"); //and able list cities = new arraylist<object>(); cities.add("belmopan"); places = cities;
so how create , when add,remove or pass list mylist action performed?
you're not going able extending arraylist
, has no built-in notification mechanism (and, further, because has been declared final
, cannot extended). however, can achieve desired result creating own list
implementation , adding "listener" functionality vis vis add()
, remove()
methods:
class mylist<t>{ private arraylist<t> list; public mylist(){ list = new arraylist<>(); ... } public void add(t t){ list.add(t) { //do other things want when items added } public t remove(t t){ list.remove(t); //do other things want when items removed
Comments
Post a Comment