How to initialize a static SparseArray
Asked Answered
O

3

9

How can I initialize a static, unmodifiable instance of android.util.SparseArray?

Oxymoron answered 5/5, 2013 at 23:0 Comment(4)
You don't initialize static properties. What exactly are you trying to do?Vito
@Eric the SparseArray equivalent of this answer https://mcmap.net/q/45459/-how-can-i-initialise-a-static-map for a static mapOxymoron
@Eric yes, exactly as I stated in this question, and the linked question/answerOxymoron
You're better off using a HashMap for this purpose. I tried tackling the same problem.Whereto
V
10

You cannot do what you are attempting to. At least, not how you are attempting to do it. There is no implementation of SparseArray that is unmodifiable.

However, you could create one. Here's how:

  • Create a class, say CustomSparseArray<E>, and have it extend SparseArray.
  • Override all methods that change the elements in the array, and replace them with something like this:

    @Override
    public void append(int key, E value) {
        if (mLocked)
            return; // Maybe throw an exception
        super.append(key, value);
    }
    
  • Then, add in a member variable to the class, boolean mLocked = false;.
  • Next, you need a method like the following:

    public void lock() { mLocked = true; }
    
  • Lastly, implement your static variable using a method similar to 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 you have an unmodifiable SparseArray in your static variable myArray.

Vito answered 6/5, 2013 at 1:43 Comment(0)
S
16

Here is a better way using an anonymous class:

static final SparseIntArray myArray = new SparseIntArray() {
    {
        append(1, 2);
        append(10, 20);
    }
};
Subpoena answered 19/12, 2013 at 2:43 Comment(0)
V
10

You cannot do what you are attempting to. At least, not how you are attempting to do it. There is no implementation of SparseArray that is unmodifiable.

However, you could create one. Here's how:

  • Create a class, say CustomSparseArray<E>, and have it extend SparseArray.
  • Override all methods that change the elements in the array, and replace them with something like this:

    @Override
    public void append(int key, E value) {
        if (mLocked)
            return; // Maybe throw an exception
        super.append(key, value);
    }
    
  • Then, add in a member variable to the class, boolean mLocked = false;.
  • Next, you need a method like the following:

    public void lock() { mLocked = true; }
    
  • Lastly, implement your static variable using a method similar to 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 you have an unmodifiable SparseArray in your static variable myArray.

Vito answered 6/5, 2013 at 1:43 Comment(0)
K
1

This works for me:

static final SparseIntArray CMyArray = new SparseIntArray();
static {
    CMyArray.append(2, 4);
    CMyArray.append(8, 3);
    CMyArray.append(255, 1);
}

as per: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

Kacerek answered 6/3, 2017 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.