java - How to initialize a static SparseArray -


how can initialize static, unmodifiable instance of android.util.sparsearray?

you cannot attempting to. @ least, not how attempting it. there no implementation of sparsearray unmodifiable.

however, create one. here's how:

  • create class, customsparsearray<e>, , have extend sparsearray.
  • override all methods change elements in array, , replace them this:

    @override public void append(int key, e value) {     if (mlocked)         return; // maybe throw exception     super.append(key, value); } 
  • then, add in member variable class, boolean mlocked = false;.
  • next, need method following:

    public void lock() { mlocked = true; } 
  • lastly, implement static variable using method similar in the other post:

    public class test {     private static final customsparsearray<integer> myarray;     static {         myarray = new customsparsearray<integer>();         myarray.append(1, 1);         myarray.append(2, 5);         myarray.lock();     } } 

then have unmodifiable sparsearray in static variable myarray.


Comments