(Java) add list of lists to ArrayList<List>












-2















I have a list of integer lists that I have initialized.



ArrayList<List<Integer>> list = new ArrayList();


I also have some data that I need to append to this list.



list = [[1,5,9], [3,5,7], [1,4,7],[2,5,8],[3,6,9], [1,2,3], [4,5,6],[7,8,9]];


EDIT:

I understand that this is not a legal way to express a list of lists in java (python background) but I do not know how to do so in Java.



What would be the best way to append this data to a list of lists in Java?










share|improve this question

























  • How is that data represented?

    – Mureinik
    Oct 19 '18 at 19:00






  • 1





    right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java

    – entercaspa
    Oct 19 '18 at 19:01
















-2















I have a list of integer lists that I have initialized.



ArrayList<List<Integer>> list = new ArrayList();


I also have some data that I need to append to this list.



list = [[1,5,9], [3,5,7], [1,4,7],[2,5,8],[3,6,9], [1,2,3], [4,5,6],[7,8,9]];


EDIT:

I understand that this is not a legal way to express a list of lists in java (python background) but I do not know how to do so in Java.



What would be the best way to append this data to a list of lists in Java?










share|improve this question

























  • How is that data represented?

    – Mureinik
    Oct 19 '18 at 19:00






  • 1





    right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java

    – entercaspa
    Oct 19 '18 at 19:01














-2












-2








-2








I have a list of integer lists that I have initialized.



ArrayList<List<Integer>> list = new ArrayList();


I also have some data that I need to append to this list.



list = [[1,5,9], [3,5,7], [1,4,7],[2,5,8],[3,6,9], [1,2,3], [4,5,6],[7,8,9]];


EDIT:

I understand that this is not a legal way to express a list of lists in java (python background) but I do not know how to do so in Java.



What would be the best way to append this data to a list of lists in Java?










share|improve this question
















I have a list of integer lists that I have initialized.



ArrayList<List<Integer>> list = new ArrayList();


I also have some data that I need to append to this list.



list = [[1,5,9], [3,5,7], [1,4,7],[2,5,8],[3,6,9], [1,2,3], [4,5,6],[7,8,9]];


EDIT:

I understand that this is not a legal way to express a list of lists in java (python background) but I do not know how to do so in Java.



What would be the best way to append this data to a list of lists in Java?







java list arraylist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 19 '18 at 19:22









Mureinik

182k22133200




182k22133200










asked Oct 19 '18 at 18:58









entercaspaentercaspa

335114




335114













  • How is that data represented?

    – Mureinik
    Oct 19 '18 at 19:00






  • 1





    right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java

    – entercaspa
    Oct 19 '18 at 19:01



















  • How is that data represented?

    – Mureinik
    Oct 19 '18 at 19:00






  • 1





    right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java

    – entercaspa
    Oct 19 '18 at 19:01

















How is that data represented?

– Mureinik
Oct 19 '18 at 19:00





How is that data represented?

– Mureinik
Oct 19 '18 at 19:00




1




1





right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java

– entercaspa
Oct 19 '18 at 19:01





right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java

– entercaspa
Oct 19 '18 at 19:01












3 Answers
3






active

oldest

votes


















2














You could do it like this



List<List<Integer>> lists = Arrays.asList(
Arrays.asList(1,5,9),
Arrays.asList(3,5,7),
Arrays.asList(1,4,7),
Arrays.asList(2,5,8),
Arrays.asList(3,6,9),
Arrays.asList(1,2,3),
Arrays.asList(4,5,6),
Arrays.asList(7,8,9)
);


if it is okay that the lists can’t change their size once they’ve been created.






share|improve this answer

































    1














    The easiest approach would probably be to use Arrays#asList:



    List<List<Integer>> list = 
    Arrays.asList(Arrays.asList(1, 5, 9),
    Arrays.asList(3, 5, 7),
    /* etc... */
    );


    EDIT:



    If you're using Java 9 or above, you could also use the slightly more elegant List#of:



    List<List<Integer>> list = 
    List.of(List.of(1, 5, 9),
    List.of(3, 5, 7),
    /* etc... */
    );





    share|improve this answer

































      0














      Using java-8 :



      List<List<Integer>> lists  = Stream
      .of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
      Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
      Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
      .collect(Collectors.toList());





      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%2f52898430%2fjava-add-list-of-lists-to-arraylistlistinteger%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        You could do it like this



        List<List<Integer>> lists = Arrays.asList(
        Arrays.asList(1,5,9),
        Arrays.asList(3,5,7),
        Arrays.asList(1,4,7),
        Arrays.asList(2,5,8),
        Arrays.asList(3,6,9),
        Arrays.asList(1,2,3),
        Arrays.asList(4,5,6),
        Arrays.asList(7,8,9)
        );


        if it is okay that the lists can’t change their size once they’ve been created.






        share|improve this answer






























          2














          You could do it like this



          List<List<Integer>> lists = Arrays.asList(
          Arrays.asList(1,5,9),
          Arrays.asList(3,5,7),
          Arrays.asList(1,4,7),
          Arrays.asList(2,5,8),
          Arrays.asList(3,6,9),
          Arrays.asList(1,2,3),
          Arrays.asList(4,5,6),
          Arrays.asList(7,8,9)
          );


          if it is okay that the lists can’t change their size once they’ve been created.






          share|improve this answer




























            2












            2








            2







            You could do it like this



            List<List<Integer>> lists = Arrays.asList(
            Arrays.asList(1,5,9),
            Arrays.asList(3,5,7),
            Arrays.asList(1,4,7),
            Arrays.asList(2,5,8),
            Arrays.asList(3,6,9),
            Arrays.asList(1,2,3),
            Arrays.asList(4,5,6),
            Arrays.asList(7,8,9)
            );


            if it is okay that the lists can’t change their size once they’ve been created.






            share|improve this answer















            You could do it like this



            List<List<Integer>> lists = Arrays.asList(
            Arrays.asList(1,5,9),
            Arrays.asList(3,5,7),
            Arrays.asList(1,4,7),
            Arrays.asList(2,5,8),
            Arrays.asList(3,6,9),
            Arrays.asList(1,2,3),
            Arrays.asList(4,5,6),
            Arrays.asList(7,8,9)
            );


            if it is okay that the lists can’t change their size once they’ve been created.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 16:42

























            answered Oct 19 '18 at 19:03









            AntimonAntimon

            1197




            1197

























                1














                The easiest approach would probably be to use Arrays#asList:



                List<List<Integer>> list = 
                Arrays.asList(Arrays.asList(1, 5, 9),
                Arrays.asList(3, 5, 7),
                /* etc... */
                );


                EDIT:



                If you're using Java 9 or above, you could also use the slightly more elegant List#of:



                List<List<Integer>> list = 
                List.of(List.of(1, 5, 9),
                List.of(3, 5, 7),
                /* etc... */
                );





                share|improve this answer






























                  1














                  The easiest approach would probably be to use Arrays#asList:



                  List<List<Integer>> list = 
                  Arrays.asList(Arrays.asList(1, 5, 9),
                  Arrays.asList(3, 5, 7),
                  /* etc... */
                  );


                  EDIT:



                  If you're using Java 9 or above, you could also use the slightly more elegant List#of:



                  List<List<Integer>> list = 
                  List.of(List.of(1, 5, 9),
                  List.of(3, 5, 7),
                  /* etc... */
                  );





                  share|improve this answer




























                    1












                    1








                    1







                    The easiest approach would probably be to use Arrays#asList:



                    List<List<Integer>> list = 
                    Arrays.asList(Arrays.asList(1, 5, 9),
                    Arrays.asList(3, 5, 7),
                    /* etc... */
                    );


                    EDIT:



                    If you're using Java 9 or above, you could also use the slightly more elegant List#of:



                    List<List<Integer>> list = 
                    List.of(List.of(1, 5, 9),
                    List.of(3, 5, 7),
                    /* etc... */
                    );





                    share|improve this answer















                    The easiest approach would probably be to use Arrays#asList:



                    List<List<Integer>> list = 
                    Arrays.asList(Arrays.asList(1, 5, 9),
                    Arrays.asList(3, 5, 7),
                    /* etc... */
                    );


                    EDIT:



                    If you're using Java 9 or above, you could also use the slightly more elegant List#of:



                    List<List<Integer>> list = 
                    List.of(List.of(1, 5, 9),
                    List.of(3, 5, 7),
                    /* etc... */
                    );






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 19 '18 at 19:10

























                    answered Oct 19 '18 at 19:04









                    MureinikMureinik

                    182k22133200




                    182k22133200























                        0














                        Using java-8 :



                        List<List<Integer>> lists  = Stream
                        .of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
                        Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
                        Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
                        .collect(Collectors.toList());





                        share|improve this answer






























                          0














                          Using java-8 :



                          List<List<Integer>> lists  = Stream
                          .of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
                          Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
                          Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
                          .collect(Collectors.toList());





                          share|improve this answer




























                            0












                            0








                            0







                            Using java-8 :



                            List<List<Integer>> lists  = Stream
                            .of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
                            Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
                            Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
                            .collect(Collectors.toList());





                            share|improve this answer















                            Using java-8 :



                            List<List<Integer>> lists  = Stream
                            .of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
                            Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
                            Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
                            .collect(Collectors.toList());






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Oct 21 '18 at 6:37

























                            answered Oct 20 '18 at 6:05









                            Nicholas KNicholas K

                            6,76761133




                            6,76761133






























                                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%2f52898430%2fjava-add-list-of-lists-to-arraylistlistinteger%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

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                Calculate evaluation metrics using cross_val_predict sklearn

                                Insert data from modal to MySQL (multiple modal on website)