You cannot do what you are trying to do. At least not how you are trying to do this. There is no SparseArray implementation that is not SparseArray .
However, you can create it. Here's how:
- Create a class, say
CustomSparseArray<E> , and extend it with a SparseArray . Override all methods that modify elements in an array and replace them in approximately the following way:
@Override public void append(int key, E value) { if (mLocked) return;
- Then add the member variable to the class,
boolean mLocked = false; . Then you need the following method:
public void lock() { mLocked = true; }
Finally, use a static variable using a method similar to another 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 you have an unmodifiable SparseArray in your static myArray variable.
Eric
source share