How to initialize a static SparseArray





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







8















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










share|improve this question




















  • 1





    You don't initialize static properties. What exactly are you trying to do?

    – Eric
    May 5 '13 at 23:01








  • 1





    @Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map

    – firyice
    May 5 '13 at 23:08








  • 1





    @Eric yes, exactly as I stated in this question, and the linked question/answer

    – firyice
    May 6 '13 at 0:30











  • You're better off using a HashMap for this purpose. I tried tackling the same problem.

    – IgorGanapolsky
    Dec 11 '15 at 16:00


















8















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










share|improve this question




















  • 1





    You don't initialize static properties. What exactly are you trying to do?

    – Eric
    May 5 '13 at 23:01








  • 1





    @Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map

    – firyice
    May 5 '13 at 23:08








  • 1





    @Eric yes, exactly as I stated in this question, and the linked question/answer

    – firyice
    May 6 '13 at 0:30











  • You're better off using a HashMap for this purpose. I tried tackling the same problem.

    – IgorGanapolsky
    Dec 11 '15 at 16:00














8












8








8


1






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










share|improve this question
















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







java android collections initialization sparse-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 6 '13 at 1:46









Eric

56.7k18111128




56.7k18111128










asked May 5 '13 at 23:00









firyicefiryice

192422




192422








  • 1





    You don't initialize static properties. What exactly are you trying to do?

    – Eric
    May 5 '13 at 23:01








  • 1





    @Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map

    – firyice
    May 5 '13 at 23:08








  • 1





    @Eric yes, exactly as I stated in this question, and the linked question/answer

    – firyice
    May 6 '13 at 0:30











  • You're better off using a HashMap for this purpose. I tried tackling the same problem.

    – IgorGanapolsky
    Dec 11 '15 at 16:00














  • 1





    You don't initialize static properties. What exactly are you trying to do?

    – Eric
    May 5 '13 at 23:01








  • 1





    @Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map

    – firyice
    May 5 '13 at 23:08








  • 1





    @Eric yes, exactly as I stated in this question, and the linked question/answer

    – firyice
    May 6 '13 at 0:30











  • You're better off using a HashMap for this purpose. I tried tackling the same problem.

    – IgorGanapolsky
    Dec 11 '15 at 16:00








1




1





You don't initialize static properties. What exactly are you trying to do?

– Eric
May 5 '13 at 23:01







You don't initialize static properties. What exactly are you trying to do?

– Eric
May 5 '13 at 23:01






1




1





@Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map

– firyice
May 5 '13 at 23:08







@Eric the SparseArray equivalent of this answer stackoverflow.com/a/507658/884677 for a static map

– firyice
May 5 '13 at 23:08






1




1





@Eric yes, exactly as I stated in this question, and the linked question/answer

– firyice
May 6 '13 at 0:30





@Eric yes, exactly as I stated in this question, and the linked question/answer

– firyice
May 6 '13 at 0:30













You're better off using a HashMap for this purpose. I tried tackling the same problem.

– IgorGanapolsky
Dec 11 '15 at 16:00





You're better off using a HashMap for this purpose. I tried tackling the same problem.

– IgorGanapolsky
Dec 11 '15 at 16:00












4 Answers
4






active

oldest

votes


















9














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.






share|improve this answer


























  • This is exactly what I was looking for. Thank you!

    – firyice
    May 6 '13 at 20:29



















16














Here is a better way using an anonymous class:



static final SparseIntArray myArray = new SparseIntArray() {
{
append(1, 2);
append(10, 20);
}
};





share|improve this answer

































    0














    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






    share|improve this answer































      0














      For unmodifiable instance of android.util.SparseArray you can use Google implementation:



      https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java






      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16389990%2fhow-to-initialize-a-static-sparsearray%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        9














        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.






        share|improve this answer


























        • This is exactly what I was looking for. Thank you!

          – firyice
          May 6 '13 at 20:29
















        9














        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.






        share|improve this answer


























        • This is exactly what I was looking for. Thank you!

          – firyice
          May 6 '13 at 20:29














        9












        9








        9







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 11:47









        Community

        11




        11










        answered May 6 '13 at 1:43









        EricEric

        56.7k18111128




        56.7k18111128













        • This is exactly what I was looking for. Thank you!

          – firyice
          May 6 '13 at 20:29



















        • This is exactly what I was looking for. Thank you!

          – firyice
          May 6 '13 at 20:29

















        This is exactly what I was looking for. Thank you!

        – firyice
        May 6 '13 at 20:29





        This is exactly what I was looking for. Thank you!

        – firyice
        May 6 '13 at 20:29













        16














        Here is a better way using an anonymous class:



        static final SparseIntArray myArray = new SparseIntArray() {
        {
        append(1, 2);
        append(10, 20);
        }
        };





        share|improve this answer






























          16














          Here is a better way using an anonymous class:



          static final SparseIntArray myArray = new SparseIntArray() {
          {
          append(1, 2);
          append(10, 20);
          }
          };





          share|improve this answer




























            16












            16








            16







            Here is a better way using an anonymous class:



            static final SparseIntArray myArray = new SparseIntArray() {
            {
            append(1, 2);
            append(10, 20);
            }
            };





            share|improve this answer















            Here is a better way using an anonymous class:



            static final SparseIntArray myArray = new SparseIntArray() {
            {
            append(1, 2);
            append(10, 20);
            }
            };






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 26 '17 at 9:16









            Hi I'm Frogatto

            20.5k86097




            20.5k86097










            answered Dec 19 '13 at 2:43









            PeterPeter

            4,38294393




            4,38294393























                0














                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






                share|improve this answer




























                  0














                  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






                  share|improve this answer


























                    0












                    0








                    0







                    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






                    share|improve this answer













                    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







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 6 '17 at 9:23









                    PJ_FinneganPJ_Finnegan

                    6931811




                    6931811























                        0














                        For unmodifiable instance of android.util.SparseArray you can use Google implementation:



                        https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java






                        share|improve this answer




























                          0














                          For unmodifiable instance of android.util.SparseArray you can use Google implementation:



                          https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java






                          share|improve this answer


























                            0












                            0








                            0







                            For unmodifiable instance of android.util.SparseArray you can use Google implementation:



                            https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java






                            share|improve this answer













                            For unmodifiable instance of android.util.SparseArray you can use Google implementation:



                            https://android.googlesource.com/platform/frameworks/base.git/+/master/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 3 at 9:28









                            intraintra

                            34036




                            34036






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16389990%2fhow-to-initialize-a-static-sparsearray%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                                Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                                A Topological Invariant for $pi_3(U(n))$