Passing int[][] as generic parameter











up vote
14
down vote

favorite
3












public static <T> void func1(T arr) {
...
}




public static <T> void func2(T arr) {
...
}


I'm trying to pass a 2-dimensional array, int arr.



I cannot use func1(arr) , but I can use func2(arr)



Can someone explain me how this works?










share|improve this question




























    up vote
    14
    down vote

    favorite
    3












    public static <T> void func1(T arr) {
    ...
    }




    public static <T> void func2(T arr) {
    ...
    }


    I'm trying to pass a 2-dimensional array, int arr.



    I cannot use func1(arr) , but I can use func2(arr)



    Can someone explain me how this works?










    share|improve this question


























      up vote
      14
      down vote

      favorite
      3









      up vote
      14
      down vote

      favorite
      3






      3





      public static <T> void func1(T arr) {
      ...
      }




      public static <T> void func2(T arr) {
      ...
      }


      I'm trying to pass a 2-dimensional array, int arr.



      I cannot use func1(arr) , but I can use func2(arr)



      Can someone explain me how this works?










      share|improve this question















      public static <T> void func1(T arr) {
      ...
      }




      public static <T> void func2(T arr) {
      ...
      }


      I'm trying to pass a 2-dimensional array, int arr.



      I cannot use func1(arr) , but I can use func2(arr)



      Can someone explain me how this works?







      java generics methods parameters parameter-passing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 9:54









      Muntasir

      6041818




      6041818










      asked Nov 17 at 21:12









      Sumit Das

      544615




      544615
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          23
          down vote













          T represents an array of some generic object. Any array type (including int) is an object. Therefore, int is a valid T when T = int.



          However, because int is not an object, int is not a valid T.






          share|improve this answer

















          • 4




            To expand on this, you could change it to Integer arr; and it should work.
            – LadyCailin
            Nov 17 at 22:28






          • 3




            @LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
            – user1643723
            Nov 18 at 6:25


















          up vote
          1
          down vote













          If you you use Integer instead of int, you should be able to:




          • call func1 with Integer arr

          • call func2 with Integer arr or Integer arr






          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',
            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%2f53355630%2fpassing-int-as-generic-parameter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            23
            down vote













            T represents an array of some generic object. Any array type (including int) is an object. Therefore, int is a valid T when T = int.



            However, because int is not an object, int is not a valid T.






            share|improve this answer

















            • 4




              To expand on this, you could change it to Integer arr; and it should work.
              – LadyCailin
              Nov 17 at 22:28






            • 3




              @LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
              – user1643723
              Nov 18 at 6:25















            up vote
            23
            down vote













            T represents an array of some generic object. Any array type (including int) is an object. Therefore, int is a valid T when T = int.



            However, because int is not an object, int is not a valid T.






            share|improve this answer

















            • 4




              To expand on this, you could change it to Integer arr; and it should work.
              – LadyCailin
              Nov 17 at 22:28






            • 3




              @LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
              – user1643723
              Nov 18 at 6:25













            up vote
            23
            down vote










            up vote
            23
            down vote









            T represents an array of some generic object. Any array type (including int) is an object. Therefore, int is a valid T when T = int.



            However, because int is not an object, int is not a valid T.






            share|improve this answer












            T represents an array of some generic object. Any array type (including int) is an object. Therefore, int is a valid T when T = int.



            However, because int is not an object, int is not a valid T.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 at 21:14









            Joe C

            10k52341




            10k52341








            • 4




              To expand on this, you could change it to Integer arr; and it should work.
              – LadyCailin
              Nov 17 at 22:28






            • 3




              @LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
              – user1643723
              Nov 18 at 6:25














            • 4




              To expand on this, you could change it to Integer arr; and it should work.
              – LadyCailin
              Nov 17 at 22:28






            • 3




              @LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
              – user1643723
              Nov 18 at 6:25








            4




            4




            To expand on this, you could change it to Integer arr; and it should work.
            – LadyCailin
            Nov 17 at 22:28




            To expand on this, you could change it to Integer arr; and it should work.
            – LadyCailin
            Nov 17 at 22:28




            3




            3




            @LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
            – user1643723
            Nov 18 at 6:25




            @LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
            – user1643723
            Nov 18 at 6:25












            up vote
            1
            down vote













            If you you use Integer instead of int, you should be able to:




            • call func1 with Integer arr

            • call func2 with Integer arr or Integer arr






            share|improve this answer



























              up vote
              1
              down vote













              If you you use Integer instead of int, you should be able to:




              • call func1 with Integer arr

              • call func2 with Integer arr or Integer arr






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                If you you use Integer instead of int, you should be able to:




                • call func1 with Integer arr

                • call func2 with Integer arr or Integer arr






                share|improve this answer














                If you you use Integer instead of int, you should be able to:




                • call func1 with Integer arr

                • call func2 with Integer arr or Integer arr







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 18 at 0:55

























                answered Nov 17 at 23:56









                TeeKea

                1,71111023




                1,71111023






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53355630%2fpassing-int-as-generic-parameter%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

                    Сан-Квентин

                    8-я гвардейская общевойсковая армия

                    Алькесар