Unmodifiable List in java -
i'm trying set list unmodifiable.
in code, have method returns list.
this list shouldn't modified, don't want catch exception returned unmodifiablelist.
private list<t> listereferenceselectall = null; list<t> olisteret = new arraylist<t>(); olisteret = listereferenceselectall; return new arraylist<t>(olisteret); it existing code , have transform return unmodifiable list, if "add" method has been called, no exception has caught.
first have create class implements list override "add" method log exception , not catch it.
but don't know how correctly instantiate it...
if absolutely must this, try follow contract specified java.util.list in list creating.
your code
public class unmodifiablearraylist<e> extends arraylist<e> { public unmodifiablearraylist(collection<? extends e> c) { super(c); } public boolean add(int index) { return false;//returning false element cannot added } public boolean addall(collection<? extends e> c) { return false;//returning false element cannot added } public e remove(int index) { return null;//returning null element cannot removed } } add more methods need on same lines. ensure constructors , methods might used modify in code overriden, ensure list unmodifiable.
using collections api cleaner , better way it, use way if using collections.unmodifiablelist not satisfy need.
and keep in mind nightmare while debugging log as possible.
Comments
Post a Comment