How to dynamically change a list value to another list in Java or Groovy












0














For Example, I have a list like below,



list1 = ["a","b","c"] now i want to add value to the list[1] index and that index will be another list like,



list2 = ["w","x","y"]



The output will be ["a",["b","w","x","y"],"c"] I don't want to replace 1 index



Is it possible?










share|improve this question




















  • 2




    What about list1.set(1, ["b","x","y"])?
    – ernest_k
    Nov 21 '18 at 7:05










  • 1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
    – masud bappy
    Nov 21 '18 at 7:10












  • or even like this: list1[1] = ["b","x","y"]
    – daggett
    Nov 21 '18 at 8:02
















0














For Example, I have a list like below,



list1 = ["a","b","c"] now i want to add value to the list[1] index and that index will be another list like,



list2 = ["w","x","y"]



The output will be ["a",["b","w","x","y"],"c"] I don't want to replace 1 index



Is it possible?










share|improve this question




















  • 2




    What about list1.set(1, ["b","x","y"])?
    – ernest_k
    Nov 21 '18 at 7:05










  • 1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
    – masud bappy
    Nov 21 '18 at 7:10












  • or even like this: list1[1] = ["b","x","y"]
    – daggett
    Nov 21 '18 at 8:02














0












0








0







For Example, I have a list like below,



list1 = ["a","b","c"] now i want to add value to the list[1] index and that index will be another list like,



list2 = ["w","x","y"]



The output will be ["a",["b","w","x","y"],"c"] I don't want to replace 1 index



Is it possible?










share|improve this question















For Example, I have a list like below,



list1 = ["a","b","c"] now i want to add value to the list[1] index and that index will be another list like,



list2 = ["w","x","y"]



The output will be ["a",["b","w","x","y"],"c"] I don't want to replace 1 index



Is it possible?







java groovy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:50







masud bappy

















asked Nov 21 '18 at 7:03









masud bappymasud bappy

33




33








  • 2




    What about list1.set(1, ["b","x","y"])?
    – ernest_k
    Nov 21 '18 at 7:05










  • 1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
    – masud bappy
    Nov 21 '18 at 7:10












  • or even like this: list1[1] = ["b","x","y"]
    – daggett
    Nov 21 '18 at 8:02














  • 2




    What about list1.set(1, ["b","x","y"])?
    – ernest_k
    Nov 21 '18 at 7:05










  • 1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
    – masud bappy
    Nov 21 '18 at 7:10












  • or even like this: list1[1] = ["b","x","y"]
    – daggett
    Nov 21 '18 at 8:02








2




2




What about list1.set(1, ["b","x","y"])?
– ernest_k
Nov 21 '18 at 7:05




What about list1.set(1, ["b","x","y"])?
– ernest_k
Nov 21 '18 at 7:05












1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
– masud bappy
Nov 21 '18 at 7:10






1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
– masud bappy
Nov 21 '18 at 7:10














or even like this: list1[1] = ["b","x","y"]
– daggett
Nov 21 '18 at 8:02




or even like this: list1[1] = ["b","x","y"]
– daggett
Nov 21 '18 at 8:02












4 Answers
4






active

oldest

votes


















0














Your base list contains two types: String and List<String>



You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.



You could use a helper method like :



private static List mergeIntoList(List source, List listToAdd, int index) {
List innerList = new ArrayList(listToAdd);
innerList.add(0, source.get(index));

List mergedList = new ArrayList(source);
mergedList.set(index, innerList);

return mergedList;
}


Exemple:



 List source = Arrays.asList("a", "b", "c");
List listToAdd = Arrays.asList("w", "x", "y");

System.out.println(mergeIntoList(source, listToAdd, 1));


output: [a, [b, w, x, y], c]



But I repeat, you should avoid raw types, so this solution is not recommended






share|improve this answer























  • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
    – masud bappy
    Nov 21 '18 at 8:02












  • @masudbappy i updated my answer because you change your question
    – Jesus Zavarce
    Nov 21 '18 at 12:08



















0














It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
When retrieving the Objects you would need to check what type they are with instanceof and then cast them.






share|improve this answer





















  • Actually i am using groovy and so far groovy is type safe.
    – masud bappy
    Nov 21 '18 at 7:22



















0














In java it is not possible taking type of list1 as String.

Type of list1 seems String and you are adding another list into list1 which is not correct.



But it is possible taking type list1 as Object



List<Object> list1=new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");

List<Object> innerlist=new ArrayList<>(); //Inner List

list1.add(innerlist);


However it is not recommended way.






share|improve this answer





























    0














    You can add value to a specific index as below



        List list = new ArrayList();
    List l1 = new ArrayList();
    l1.add("3");
    l1.add("4");
    list.add("1");
    list.add("5");
    list.add(1, l1);


    Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException



    Please try below logic



    public List replaceIndex(List original, List replace, int index) {
    Object object = original.remove(index);
    replace.add(0, object);
    original.add(index, replace);
    return original;
    }





    share|improve this answer























    • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
      – masud bappy
      Nov 21 '18 at 8:05










    • @masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
      – darshakat
      Nov 21 '18 at 8:33











    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%2f53406827%2fhow-to-dynamically-change-a-list-value-to-another-list-in-java-or-groovy%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









    0














    Your base list contains two types: String and List<String>



    You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.



    You could use a helper method like :



    private static List mergeIntoList(List source, List listToAdd, int index) {
    List innerList = new ArrayList(listToAdd);
    innerList.add(0, source.get(index));

    List mergedList = new ArrayList(source);
    mergedList.set(index, innerList);

    return mergedList;
    }


    Exemple:



     List source = Arrays.asList("a", "b", "c");
    List listToAdd = Arrays.asList("w", "x", "y");

    System.out.println(mergeIntoList(source, listToAdd, 1));


    output: [a, [b, w, x, y], c]



    But I repeat, you should avoid raw types, so this solution is not recommended






    share|improve this answer























    • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
      – masud bappy
      Nov 21 '18 at 8:02












    • @masudbappy i updated my answer because you change your question
      – Jesus Zavarce
      Nov 21 '18 at 12:08
















    0














    Your base list contains two types: String and List<String>



    You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.



    You could use a helper method like :



    private static List mergeIntoList(List source, List listToAdd, int index) {
    List innerList = new ArrayList(listToAdd);
    innerList.add(0, source.get(index));

    List mergedList = new ArrayList(source);
    mergedList.set(index, innerList);

    return mergedList;
    }


    Exemple:



     List source = Arrays.asList("a", "b", "c");
    List listToAdd = Arrays.asList("w", "x", "y");

    System.out.println(mergeIntoList(source, listToAdd, 1));


    output: [a, [b, w, x, y], c]



    But I repeat, you should avoid raw types, so this solution is not recommended






    share|improve this answer























    • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
      – masud bappy
      Nov 21 '18 at 8:02












    • @masudbappy i updated my answer because you change your question
      – Jesus Zavarce
      Nov 21 '18 at 12:08














    0












    0








    0






    Your base list contains two types: String and List<String>



    You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.



    You could use a helper method like :



    private static List mergeIntoList(List source, List listToAdd, int index) {
    List innerList = new ArrayList(listToAdd);
    innerList.add(0, source.get(index));

    List mergedList = new ArrayList(source);
    mergedList.set(index, innerList);

    return mergedList;
    }


    Exemple:



     List source = Arrays.asList("a", "b", "c");
    List listToAdd = Arrays.asList("w", "x", "y");

    System.out.println(mergeIntoList(source, listToAdd, 1));


    output: [a, [b, w, x, y], c]



    But I repeat, you should avoid raw types, so this solution is not recommended






    share|improve this answer














    Your base list contains two types: String and List<String>



    You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.



    You could use a helper method like :



    private static List mergeIntoList(List source, List listToAdd, int index) {
    List innerList = new ArrayList(listToAdd);
    innerList.add(0, source.get(index));

    List mergedList = new ArrayList(source);
    mergedList.set(index, innerList);

    return mergedList;
    }


    Exemple:



     List source = Arrays.asList("a", "b", "c");
    List listToAdd = Arrays.asList("w", "x", "y");

    System.out.println(mergeIntoList(source, listToAdd, 1));


    output: [a, [b, w, x, y], c]



    But I repeat, you should avoid raw types, so this solution is not recommended







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 12:07

























    answered Nov 21 '18 at 7:44









    Jesus ZavarceJesus Zavarce

    1,049818




    1,049818












    • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
      – masud bappy
      Nov 21 '18 at 8:02












    • @masudbappy i updated my answer because you change your question
      – Jesus Zavarce
      Nov 21 '18 at 12:08


















    • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
      – masud bappy
      Nov 21 '18 at 8:02












    • @masudbappy i updated my answer because you change your question
      – Jesus Zavarce
      Nov 21 '18 at 12:08
















    def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
    – masud bappy
    Nov 21 '18 at 8:02






    def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
    – masud bappy
    Nov 21 '18 at 8:02














    @masudbappy i updated my answer because you change your question
    – Jesus Zavarce
    Nov 21 '18 at 12:08




    @masudbappy i updated my answer because you change your question
    – Jesus Zavarce
    Nov 21 '18 at 12:08













    0














    It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
    When retrieving the Objects you would need to check what type they are with instanceof and then cast them.






    share|improve this answer





















    • Actually i am using groovy and so far groovy is type safe.
      – masud bappy
      Nov 21 '18 at 7:22
















    0














    It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
    When retrieving the Objects you would need to check what type they are with instanceof and then cast them.






    share|improve this answer





















    • Actually i am using groovy and so far groovy is type safe.
      – masud bappy
      Nov 21 '18 at 7:22














    0












    0








    0






    It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
    When retrieving the Objects you would need to check what type they are with instanceof and then cast them.






    share|improve this answer












    It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
    When retrieving the Objects you would need to check what type they are with instanceof and then cast them.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 7:18









    Andreas HartmannAndreas Hartmann

    7791024




    7791024












    • Actually i am using groovy and so far groovy is type safe.
      – masud bappy
      Nov 21 '18 at 7:22


















    • Actually i am using groovy and so far groovy is type safe.
      – masud bappy
      Nov 21 '18 at 7:22
















    Actually i am using groovy and so far groovy is type safe.
    – masud bappy
    Nov 21 '18 at 7:22




    Actually i am using groovy and so far groovy is type safe.
    – masud bappy
    Nov 21 '18 at 7:22











    0














    In java it is not possible taking type of list1 as String.

    Type of list1 seems String and you are adding another list into list1 which is not correct.



    But it is possible taking type list1 as Object



    List<Object> list1=new ArrayList<>();
    list1.add("a");
    list1.add("b");
    list1.add("c");

    List<Object> innerlist=new ArrayList<>(); //Inner List

    list1.add(innerlist);


    However it is not recommended way.






    share|improve this answer


























      0














      In java it is not possible taking type of list1 as String.

      Type of list1 seems String and you are adding another list into list1 which is not correct.



      But it is possible taking type list1 as Object



      List<Object> list1=new ArrayList<>();
      list1.add("a");
      list1.add("b");
      list1.add("c");

      List<Object> innerlist=new ArrayList<>(); //Inner List

      list1.add(innerlist);


      However it is not recommended way.






      share|improve this answer
























        0












        0








        0






        In java it is not possible taking type of list1 as String.

        Type of list1 seems String and you are adding another list into list1 which is not correct.



        But it is possible taking type list1 as Object



        List<Object> list1=new ArrayList<>();
        list1.add("a");
        list1.add("b");
        list1.add("c");

        List<Object> innerlist=new ArrayList<>(); //Inner List

        list1.add(innerlist);


        However it is not recommended way.






        share|improve this answer












        In java it is not possible taking type of list1 as String.

        Type of list1 seems String and you are adding another list into list1 which is not correct.



        But it is possible taking type list1 as Object



        List<Object> list1=new ArrayList<>();
        list1.add("a");
        list1.add("b");
        list1.add("c");

        List<Object> innerlist=new ArrayList<>(); //Inner List

        list1.add(innerlist);


        However it is not recommended way.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 7:23









        TarunTarun

        649414




        649414























            0














            You can add value to a specific index as below



                List list = new ArrayList();
            List l1 = new ArrayList();
            l1.add("3");
            l1.add("4");
            list.add("1");
            list.add("5");
            list.add(1, l1);


            Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException



            Please try below logic



            public List replaceIndex(List original, List replace, int index) {
            Object object = original.remove(index);
            replace.add(0, object);
            original.add(index, replace);
            return original;
            }





            share|improve this answer























            • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
              – masud bappy
              Nov 21 '18 at 8:05










            • @masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
              – darshakat
              Nov 21 '18 at 8:33
















            0














            You can add value to a specific index as below



                List list = new ArrayList();
            List l1 = new ArrayList();
            l1.add("3");
            l1.add("4");
            list.add("1");
            list.add("5");
            list.add(1, l1);


            Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException



            Please try below logic



            public List replaceIndex(List original, List replace, int index) {
            Object object = original.remove(index);
            replace.add(0, object);
            original.add(index, replace);
            return original;
            }





            share|improve this answer























            • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
              – masud bappy
              Nov 21 '18 at 8:05










            • @masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
              – darshakat
              Nov 21 '18 at 8:33














            0












            0








            0






            You can add value to a specific index as below



                List list = new ArrayList();
            List l1 = new ArrayList();
            l1.add("3");
            l1.add("4");
            list.add("1");
            list.add("5");
            list.add(1, l1);


            Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException



            Please try below logic



            public List replaceIndex(List original, List replace, int index) {
            Object object = original.remove(index);
            replace.add(0, object);
            original.add(index, replace);
            return original;
            }





            share|improve this answer














            You can add value to a specific index as below



                List list = new ArrayList();
            List l1 = new ArrayList();
            l1.add("3");
            l1.add("4");
            list.add("1");
            list.add("5");
            list.add(1, l1);


            Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException



            Please try below logic



            public List replaceIndex(List original, List replace, int index) {
            Object object = original.remove(index);
            replace.add(0, object);
            original.add(index, replace);
            return original;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 8:33

























            answered Nov 21 '18 at 7:32









            darshakatdarshakat

            366110




            366110












            • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
              – masud bappy
              Nov 21 '18 at 8:05










            • @masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
              – darshakat
              Nov 21 '18 at 8:33


















            • def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
              – masud bappy
              Nov 21 '18 at 8:05










            • @masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
              – darshakat
              Nov 21 '18 at 8:33
















            def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
            – masud bappy
            Nov 21 '18 at 8:05




            def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
            – masud bappy
            Nov 21 '18 at 8:05












            @masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
            – darshakat
            Nov 21 '18 at 8:33




            @masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
            – darshakat
            Nov 21 '18 at 8:33


















            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%2f53406827%2fhow-to-dynamically-change-a-list-value-to-another-list-in-java-or-groovy%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin