a list of strings in Java





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







-4















I have a question about setting the value to the list of strings in Java.



For example, if you have a list of strings called names, how would you set the 3rd name to the value "San Diego"?



I tried names.add(2, "San Diego") but it's not working.



Thanks so much for the help!










share|improve this question























  • Also, how would I print the 3rd item from a list called students?

    – Jamie_dogLover
    Nov 23 '18 at 8:06






  • 1





    Try to use set instead of add . To read it you can use get(2) .

    – Arnaud
    Nov 23 '18 at 8:06













  • It would depend on the container class, but it’s based java.util.List, then it’s set

    – MadProgrammer
    Nov 23 '18 at 8:08






  • 1





    Oh, add you should consult the JavaDocs first 😉

    – MadProgrammer
    Nov 23 '18 at 8:10






  • 1





    Please read How to Ask. You are supposed to do proper research before asking a question at StackOverflow. This includes reading through the official documentation of a class you are using. The documentation explains all of those methods in detail, and gives examples. Alternatively, you can also search on google and probably find a lot of tutorials and examples on how to use List. Likely, this is also the reason for the down-votes.

    – Zabuza
    Nov 23 '18 at 8:10


















-4















I have a question about setting the value to the list of strings in Java.



For example, if you have a list of strings called names, how would you set the 3rd name to the value "San Diego"?



I tried names.add(2, "San Diego") but it's not working.



Thanks so much for the help!










share|improve this question























  • Also, how would I print the 3rd item from a list called students?

    – Jamie_dogLover
    Nov 23 '18 at 8:06






  • 1





    Try to use set instead of add . To read it you can use get(2) .

    – Arnaud
    Nov 23 '18 at 8:06













  • It would depend on the container class, but it’s based java.util.List, then it’s set

    – MadProgrammer
    Nov 23 '18 at 8:08






  • 1





    Oh, add you should consult the JavaDocs first 😉

    – MadProgrammer
    Nov 23 '18 at 8:10






  • 1





    Please read How to Ask. You are supposed to do proper research before asking a question at StackOverflow. This includes reading through the official documentation of a class you are using. The documentation explains all of those methods in detail, and gives examples. Alternatively, you can also search on google and probably find a lot of tutorials and examples on how to use List. Likely, this is also the reason for the down-votes.

    – Zabuza
    Nov 23 '18 at 8:10














-4












-4








-4








I have a question about setting the value to the list of strings in Java.



For example, if you have a list of strings called names, how would you set the 3rd name to the value "San Diego"?



I tried names.add(2, "San Diego") but it's not working.



Thanks so much for the help!










share|improve this question














I have a question about setting the value to the list of strings in Java.



For example, if you have a list of strings called names, how would you set the 3rd name to the value "San Diego"?



I tried names.add(2, "San Diego") but it's not working.



Thanks so much for the help!







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 8:04









Jamie_dogLoverJamie_dogLover

6




6













  • Also, how would I print the 3rd item from a list called students?

    – Jamie_dogLover
    Nov 23 '18 at 8:06






  • 1





    Try to use set instead of add . To read it you can use get(2) .

    – Arnaud
    Nov 23 '18 at 8:06













  • It would depend on the container class, but it’s based java.util.List, then it’s set

    – MadProgrammer
    Nov 23 '18 at 8:08






  • 1





    Oh, add you should consult the JavaDocs first 😉

    – MadProgrammer
    Nov 23 '18 at 8:10






  • 1





    Please read How to Ask. You are supposed to do proper research before asking a question at StackOverflow. This includes reading through the official documentation of a class you are using. The documentation explains all of those methods in detail, and gives examples. Alternatively, you can also search on google and probably find a lot of tutorials and examples on how to use List. Likely, this is also the reason for the down-votes.

    – Zabuza
    Nov 23 '18 at 8:10



















  • Also, how would I print the 3rd item from a list called students?

    – Jamie_dogLover
    Nov 23 '18 at 8:06






  • 1





    Try to use set instead of add . To read it you can use get(2) .

    – Arnaud
    Nov 23 '18 at 8:06













  • It would depend on the container class, but it’s based java.util.List, then it’s set

    – MadProgrammer
    Nov 23 '18 at 8:08






  • 1





    Oh, add you should consult the JavaDocs first 😉

    – MadProgrammer
    Nov 23 '18 at 8:10






  • 1





    Please read How to Ask. You are supposed to do proper research before asking a question at StackOverflow. This includes reading through the official documentation of a class you are using. The documentation explains all of those methods in detail, and gives examples. Alternatively, you can also search on google and probably find a lot of tutorials and examples on how to use List. Likely, this is also the reason for the down-votes.

    – Zabuza
    Nov 23 '18 at 8:10

















Also, how would I print the 3rd item from a list called students?

– Jamie_dogLover
Nov 23 '18 at 8:06





Also, how would I print the 3rd item from a list called students?

– Jamie_dogLover
Nov 23 '18 at 8:06




1




1





Try to use set instead of add . To read it you can use get(2) .

– Arnaud
Nov 23 '18 at 8:06







Try to use set instead of add . To read it you can use get(2) .

– Arnaud
Nov 23 '18 at 8:06















It would depend on the container class, but it’s based java.util.List, then it’s set

– MadProgrammer
Nov 23 '18 at 8:08





It would depend on the container class, but it’s based java.util.List, then it’s set

– MadProgrammer
Nov 23 '18 at 8:08




1




1





Oh, add you should consult the JavaDocs first 😉

– MadProgrammer
Nov 23 '18 at 8:10





Oh, add you should consult the JavaDocs first 😉

– MadProgrammer
Nov 23 '18 at 8:10




1




1





Please read How to Ask. You are supposed to do proper research before asking a question at StackOverflow. This includes reading through the official documentation of a class you are using. The documentation explains all of those methods in detail, and gives examples. Alternatively, you can also search on google and probably find a lot of tutorials and examples on how to use List. Likely, this is also the reason for the down-votes.

– Zabuza
Nov 23 '18 at 8:10





Please read How to Ask. You are supposed to do proper research before asking a question at StackOverflow. This includes reading through the official documentation of a class you are using. The documentation explains all of those methods in detail, and gives examples. Alternatively, you can also search on google and probably find a lot of tutorials and examples on how to use List. Likely, this is also the reason for the down-votes.

– Zabuza
Nov 23 '18 at 8:10












5 Answers
5






active

oldest

votes


















2














To update the already existing value use names.set(2,"String");






share|improve this answer
























  • Gotcha! Thank you, sir. If I want to print the 3rd item from the student list. Can I do System.out.printIn(students.indexOf(2))?

    – Jamie_dogLover
    Nov 23 '18 at 8:13











  • use students.get(2); indexOf method will return the index of the object that you pass as the argument if the object is present in the list students. e.g names.indexOf ("San Diego") will return 2 in your case

    – Deepak Gunasekaran
    Nov 23 '18 at 8:16





















2














names.add(2, "San Diego") will add a new element at the 3rd position (index 2), but it will only work if the List already contains at least 2 elements.



names.set(2, "San Diego") will set the 3rd element (at index 2) to the required value, but it will work only if the List already contains at least 3 elements.



To obtain the 3rd element, you should use names.get(2) (again, the List must contain at least 3 elements for this to work).






share|improve this answer
























  • Thank you so much!

    – Jamie_dogLover
    Nov 23 '18 at 8:16



















1














If you want to replace an existing item in a list, you have to use set instead of add



names.set(2,"San Diego");


In here 2 means the index, counting from 0.

Then is the String you want to add



If you want to read values from the list you can use get



String value = names.get(2);





share|improve this answer































    0














    it will work if the List already contains at least 2 elements.



    names.add(2, "San Diego") it will override the 3rd element or at index 2. 


    it is always safe to use



    names.set(2, "San Diego") it will set the 3rd value or at index 2 if the list contains at least 3 elements. 


    Otherwise, it will throw IndexOutOfBoundsException: exception.






    share|improve this answer































      0














      Here are the basic operation you can perform there are plenty of operation you can perform.
      One advice try try and try dont give up.
      here the is link for java dock for your reference
      Java doc for List






      package com.varun.list;

      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;

      public class ArrayListTest {

      public static void main(String args) {

      //Create array list
      List < String > names = new ArrayList < String > ();

      //Create few names
      String sam = "Sam";
      String jamie = "Jamie_dogLover";
      String bucy = "The Bucky";

      // add all names in the list
      names.add(sam);
      names.add(jamie);
      names.add(bucy);
      System.out.println("List values :" + names); //Output : List values :[Sam, Jamie_dogLover, The Bucky]

      //Update the second index that is -> The Bucky, because list index starts from 0
      names.set(2, "New Bucky");
      System.out.println("New List values :" + names); //Output : New List values :[Sam, Jamie_dogLover, New Bucky]

      //Lest say you want to remove the the 1st index that is jamie
      names.remove(jamie);
      System.out.println("Updates List values :" + names); //Output : Updates List values :[Sam, New Bucky]

      //Let say you get a list of name from somewhere and you want to add into you existing list(names)
      names.addAll(listOfDummyNames());

      System.out.println("Updates List values with dummy names :" + names);
      //Output : Updates List values with dummy names :[Sam, New Bucky, Name0, Name1, Name2, Name3, Name4]
      //Let say you want to to remove all the name who has "name" String in it

      Iterator < String > itr = names.iterator();
      while (itr.hasNext()) {
      String name = itr.next();
      if (name.contains("Name")) {
      itr.remove();
      }
      }

      System.out.println("List after removing all name who has Name in it : " + names);

      }

      private static List < String > listOfDummyNames() {

      List < String > dummyList = new ArrayList < String > ();
      String name = "Name";
      for (int i = 0; i < 5; i++) {
      dummyList.add(name + i);
      }

      System.out.println("List of dummy Names :" + dummyList);
      return dummyList;
      }

      }








      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%2f53442735%2fa-list-of-strings-in-java%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        To update the already existing value use names.set(2,"String");






        share|improve this answer
























        • Gotcha! Thank you, sir. If I want to print the 3rd item from the student list. Can I do System.out.printIn(students.indexOf(2))?

          – Jamie_dogLover
          Nov 23 '18 at 8:13











        • use students.get(2); indexOf method will return the index of the object that you pass as the argument if the object is present in the list students. e.g names.indexOf ("San Diego") will return 2 in your case

          – Deepak Gunasekaran
          Nov 23 '18 at 8:16


















        2














        To update the already existing value use names.set(2,"String");






        share|improve this answer
























        • Gotcha! Thank you, sir. If I want to print the 3rd item from the student list. Can I do System.out.printIn(students.indexOf(2))?

          – Jamie_dogLover
          Nov 23 '18 at 8:13











        • use students.get(2); indexOf method will return the index of the object that you pass as the argument if the object is present in the list students. e.g names.indexOf ("San Diego") will return 2 in your case

          – Deepak Gunasekaran
          Nov 23 '18 at 8:16
















        2












        2








        2







        To update the already existing value use names.set(2,"String");






        share|improve this answer













        To update the already existing value use names.set(2,"String");







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 8:06









        Deepak GunasekaranDeepak Gunasekaran

        688212




        688212













        • Gotcha! Thank you, sir. If I want to print the 3rd item from the student list. Can I do System.out.printIn(students.indexOf(2))?

          – Jamie_dogLover
          Nov 23 '18 at 8:13











        • use students.get(2); indexOf method will return the index of the object that you pass as the argument if the object is present in the list students. e.g names.indexOf ("San Diego") will return 2 in your case

          – Deepak Gunasekaran
          Nov 23 '18 at 8:16





















        • Gotcha! Thank you, sir. If I want to print the 3rd item from the student list. Can I do System.out.printIn(students.indexOf(2))?

          – Jamie_dogLover
          Nov 23 '18 at 8:13











        • use students.get(2); indexOf method will return the index of the object that you pass as the argument if the object is present in the list students. e.g names.indexOf ("San Diego") will return 2 in your case

          – Deepak Gunasekaran
          Nov 23 '18 at 8:16



















        Gotcha! Thank you, sir. If I want to print the 3rd item from the student list. Can I do System.out.printIn(students.indexOf(2))?

        – Jamie_dogLover
        Nov 23 '18 at 8:13





        Gotcha! Thank you, sir. If I want to print the 3rd item from the student list. Can I do System.out.printIn(students.indexOf(2))?

        – Jamie_dogLover
        Nov 23 '18 at 8:13













        use students.get(2); indexOf method will return the index of the object that you pass as the argument if the object is present in the list students. e.g names.indexOf ("San Diego") will return 2 in your case

        – Deepak Gunasekaran
        Nov 23 '18 at 8:16







        use students.get(2); indexOf method will return the index of the object that you pass as the argument if the object is present in the list students. e.g names.indexOf ("San Diego") will return 2 in your case

        – Deepak Gunasekaran
        Nov 23 '18 at 8:16















        2














        names.add(2, "San Diego") will add a new element at the 3rd position (index 2), but it will only work if the List already contains at least 2 elements.



        names.set(2, "San Diego") will set the 3rd element (at index 2) to the required value, but it will work only if the List already contains at least 3 elements.



        To obtain the 3rd element, you should use names.get(2) (again, the List must contain at least 3 elements for this to work).






        share|improve this answer
























        • Thank you so much!

          – Jamie_dogLover
          Nov 23 '18 at 8:16
















        2














        names.add(2, "San Diego") will add a new element at the 3rd position (index 2), but it will only work if the List already contains at least 2 elements.



        names.set(2, "San Diego") will set the 3rd element (at index 2) to the required value, but it will work only if the List already contains at least 3 elements.



        To obtain the 3rd element, you should use names.get(2) (again, the List must contain at least 3 elements for this to work).






        share|improve this answer
























        • Thank you so much!

          – Jamie_dogLover
          Nov 23 '18 at 8:16














        2












        2








        2







        names.add(2, "San Diego") will add a new element at the 3rd position (index 2), but it will only work if the List already contains at least 2 elements.



        names.set(2, "San Diego") will set the 3rd element (at index 2) to the required value, but it will work only if the List already contains at least 3 elements.



        To obtain the 3rd element, you should use names.get(2) (again, the List must contain at least 3 elements for this to work).






        share|improve this answer













        names.add(2, "San Diego") will add a new element at the 3rd position (index 2), but it will only work if the List already contains at least 2 elements.



        names.set(2, "San Diego") will set the 3rd element (at index 2) to the required value, but it will work only if the List already contains at least 3 elements.



        To obtain the 3rd element, you should use names.get(2) (again, the List must contain at least 3 elements for this to work).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 8:09









        EranEran

        293k37485565




        293k37485565













        • Thank you so much!

          – Jamie_dogLover
          Nov 23 '18 at 8:16



















        • Thank you so much!

          – Jamie_dogLover
          Nov 23 '18 at 8:16

















        Thank you so much!

        – Jamie_dogLover
        Nov 23 '18 at 8:16





        Thank you so much!

        – Jamie_dogLover
        Nov 23 '18 at 8:16











        1














        If you want to replace an existing item in a list, you have to use set instead of add



        names.set(2,"San Diego");


        In here 2 means the index, counting from 0.

        Then is the String you want to add



        If you want to read values from the list you can use get



        String value = names.get(2);





        share|improve this answer




























          1














          If you want to replace an existing item in a list, you have to use set instead of add



          names.set(2,"San Diego");


          In here 2 means the index, counting from 0.

          Then is the String you want to add



          If you want to read values from the list you can use get



          String value = names.get(2);





          share|improve this answer


























            1












            1








            1







            If you want to replace an existing item in a list, you have to use set instead of add



            names.set(2,"San Diego");


            In here 2 means the index, counting from 0.

            Then is the String you want to add



            If you want to read values from the list you can use get



            String value = names.get(2);





            share|improve this answer













            If you want to replace an existing item in a list, you have to use set instead of add



            names.set(2,"San Diego");


            In here 2 means the index, counting from 0.

            Then is the String you want to add



            If you want to read values from the list you can use get



            String value = names.get(2);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 8:08









            Gihan Saranga SiriwardhanaGihan Saranga Siriwardhana

            625425




            625425























                0














                it will work if the List already contains at least 2 elements.



                names.add(2, "San Diego") it will override the 3rd element or at index 2. 


                it is always safe to use



                names.set(2, "San Diego") it will set the 3rd value or at index 2 if the list contains at least 3 elements. 


                Otherwise, it will throw IndexOutOfBoundsException: exception.






                share|improve this answer




























                  0














                  it will work if the List already contains at least 2 elements.



                  names.add(2, "San Diego") it will override the 3rd element or at index 2. 


                  it is always safe to use



                  names.set(2, "San Diego") it will set the 3rd value or at index 2 if the list contains at least 3 elements. 


                  Otherwise, it will throw IndexOutOfBoundsException: exception.






                  share|improve this answer


























                    0












                    0








                    0







                    it will work if the List already contains at least 2 elements.



                    names.add(2, "San Diego") it will override the 3rd element or at index 2. 


                    it is always safe to use



                    names.set(2, "San Diego") it will set the 3rd value or at index 2 if the list contains at least 3 elements. 


                    Otherwise, it will throw IndexOutOfBoundsException: exception.






                    share|improve this answer













                    it will work if the List already contains at least 2 elements.



                    names.add(2, "San Diego") it will override the 3rd element or at index 2. 


                    it is always safe to use



                    names.set(2, "San Diego") it will set the 3rd value or at index 2 if the list contains at least 3 elements. 


                    Otherwise, it will throw IndexOutOfBoundsException: exception.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 8:33









                    saurav omarsaurav omar

                    767




                    767























                        0














                        Here are the basic operation you can perform there are plenty of operation you can perform.
                        One advice try try and try dont give up.
                        here the is link for java dock for your reference
                        Java doc for List






                        package com.varun.list;

                        import java.util.ArrayList;
                        import java.util.Iterator;
                        import java.util.List;

                        public class ArrayListTest {

                        public static void main(String args) {

                        //Create array list
                        List < String > names = new ArrayList < String > ();

                        //Create few names
                        String sam = "Sam";
                        String jamie = "Jamie_dogLover";
                        String bucy = "The Bucky";

                        // add all names in the list
                        names.add(sam);
                        names.add(jamie);
                        names.add(bucy);
                        System.out.println("List values :" + names); //Output : List values :[Sam, Jamie_dogLover, The Bucky]

                        //Update the second index that is -> The Bucky, because list index starts from 0
                        names.set(2, "New Bucky");
                        System.out.println("New List values :" + names); //Output : New List values :[Sam, Jamie_dogLover, New Bucky]

                        //Lest say you want to remove the the 1st index that is jamie
                        names.remove(jamie);
                        System.out.println("Updates List values :" + names); //Output : Updates List values :[Sam, New Bucky]

                        //Let say you get a list of name from somewhere and you want to add into you existing list(names)
                        names.addAll(listOfDummyNames());

                        System.out.println("Updates List values with dummy names :" + names);
                        //Output : Updates List values with dummy names :[Sam, New Bucky, Name0, Name1, Name2, Name3, Name4]
                        //Let say you want to to remove all the name who has "name" String in it

                        Iterator < String > itr = names.iterator();
                        while (itr.hasNext()) {
                        String name = itr.next();
                        if (name.contains("Name")) {
                        itr.remove();
                        }
                        }

                        System.out.println("List after removing all name who has Name in it : " + names);

                        }

                        private static List < String > listOfDummyNames() {

                        List < String > dummyList = new ArrayList < String > ();
                        String name = "Name";
                        for (int i = 0; i < 5; i++) {
                        dummyList.add(name + i);
                        }

                        System.out.println("List of dummy Names :" + dummyList);
                        return dummyList;
                        }

                        }








                        share|improve this answer




























                          0














                          Here are the basic operation you can perform there are plenty of operation you can perform.
                          One advice try try and try dont give up.
                          here the is link for java dock for your reference
                          Java doc for List






                          package com.varun.list;

                          import java.util.ArrayList;
                          import java.util.Iterator;
                          import java.util.List;

                          public class ArrayListTest {

                          public static void main(String args) {

                          //Create array list
                          List < String > names = new ArrayList < String > ();

                          //Create few names
                          String sam = "Sam";
                          String jamie = "Jamie_dogLover";
                          String bucy = "The Bucky";

                          // add all names in the list
                          names.add(sam);
                          names.add(jamie);
                          names.add(bucy);
                          System.out.println("List values :" + names); //Output : List values :[Sam, Jamie_dogLover, The Bucky]

                          //Update the second index that is -> The Bucky, because list index starts from 0
                          names.set(2, "New Bucky");
                          System.out.println("New List values :" + names); //Output : New List values :[Sam, Jamie_dogLover, New Bucky]

                          //Lest say you want to remove the the 1st index that is jamie
                          names.remove(jamie);
                          System.out.println("Updates List values :" + names); //Output : Updates List values :[Sam, New Bucky]

                          //Let say you get a list of name from somewhere and you want to add into you existing list(names)
                          names.addAll(listOfDummyNames());

                          System.out.println("Updates List values with dummy names :" + names);
                          //Output : Updates List values with dummy names :[Sam, New Bucky, Name0, Name1, Name2, Name3, Name4]
                          //Let say you want to to remove all the name who has "name" String in it

                          Iterator < String > itr = names.iterator();
                          while (itr.hasNext()) {
                          String name = itr.next();
                          if (name.contains("Name")) {
                          itr.remove();
                          }
                          }

                          System.out.println("List after removing all name who has Name in it : " + names);

                          }

                          private static List < String > listOfDummyNames() {

                          List < String > dummyList = new ArrayList < String > ();
                          String name = "Name";
                          for (int i = 0; i < 5; i++) {
                          dummyList.add(name + i);
                          }

                          System.out.println("List of dummy Names :" + dummyList);
                          return dummyList;
                          }

                          }








                          share|improve this answer


























                            0












                            0








                            0







                            Here are the basic operation you can perform there are plenty of operation you can perform.
                            One advice try try and try dont give up.
                            here the is link for java dock for your reference
                            Java doc for List






                            package com.varun.list;

                            import java.util.ArrayList;
                            import java.util.Iterator;
                            import java.util.List;

                            public class ArrayListTest {

                            public static void main(String args) {

                            //Create array list
                            List < String > names = new ArrayList < String > ();

                            //Create few names
                            String sam = "Sam";
                            String jamie = "Jamie_dogLover";
                            String bucy = "The Bucky";

                            // add all names in the list
                            names.add(sam);
                            names.add(jamie);
                            names.add(bucy);
                            System.out.println("List values :" + names); //Output : List values :[Sam, Jamie_dogLover, The Bucky]

                            //Update the second index that is -> The Bucky, because list index starts from 0
                            names.set(2, "New Bucky");
                            System.out.println("New List values :" + names); //Output : New List values :[Sam, Jamie_dogLover, New Bucky]

                            //Lest say you want to remove the the 1st index that is jamie
                            names.remove(jamie);
                            System.out.println("Updates List values :" + names); //Output : Updates List values :[Sam, New Bucky]

                            //Let say you get a list of name from somewhere and you want to add into you existing list(names)
                            names.addAll(listOfDummyNames());

                            System.out.println("Updates List values with dummy names :" + names);
                            //Output : Updates List values with dummy names :[Sam, New Bucky, Name0, Name1, Name2, Name3, Name4]
                            //Let say you want to to remove all the name who has "name" String in it

                            Iterator < String > itr = names.iterator();
                            while (itr.hasNext()) {
                            String name = itr.next();
                            if (name.contains("Name")) {
                            itr.remove();
                            }
                            }

                            System.out.println("List after removing all name who has Name in it : " + names);

                            }

                            private static List < String > listOfDummyNames() {

                            List < String > dummyList = new ArrayList < String > ();
                            String name = "Name";
                            for (int i = 0; i < 5; i++) {
                            dummyList.add(name + i);
                            }

                            System.out.println("List of dummy Names :" + dummyList);
                            return dummyList;
                            }

                            }








                            share|improve this answer













                            Here are the basic operation you can perform there are plenty of operation you can perform.
                            One advice try try and try dont give up.
                            here the is link for java dock for your reference
                            Java doc for List






                            package com.varun.list;

                            import java.util.ArrayList;
                            import java.util.Iterator;
                            import java.util.List;

                            public class ArrayListTest {

                            public static void main(String args) {

                            //Create array list
                            List < String > names = new ArrayList < String > ();

                            //Create few names
                            String sam = "Sam";
                            String jamie = "Jamie_dogLover";
                            String bucy = "The Bucky";

                            // add all names in the list
                            names.add(sam);
                            names.add(jamie);
                            names.add(bucy);
                            System.out.println("List values :" + names); //Output : List values :[Sam, Jamie_dogLover, The Bucky]

                            //Update the second index that is -> The Bucky, because list index starts from 0
                            names.set(2, "New Bucky");
                            System.out.println("New List values :" + names); //Output : New List values :[Sam, Jamie_dogLover, New Bucky]

                            //Lest say you want to remove the the 1st index that is jamie
                            names.remove(jamie);
                            System.out.println("Updates List values :" + names); //Output : Updates List values :[Sam, New Bucky]

                            //Let say you get a list of name from somewhere and you want to add into you existing list(names)
                            names.addAll(listOfDummyNames());

                            System.out.println("Updates List values with dummy names :" + names);
                            //Output : Updates List values with dummy names :[Sam, New Bucky, Name0, Name1, Name2, Name3, Name4]
                            //Let say you want to to remove all the name who has "name" String in it

                            Iterator < String > itr = names.iterator();
                            while (itr.hasNext()) {
                            String name = itr.next();
                            if (name.contains("Name")) {
                            itr.remove();
                            }
                            }

                            System.out.println("List after removing all name who has Name in it : " + names);

                            }

                            private static List < String > listOfDummyNames() {

                            List < String > dummyList = new ArrayList < String > ();
                            String name = "Name";
                            for (int i = 0; i < 5; i++) {
                            dummyList.add(name + i);
                            }

                            System.out.println("List of dummy Names :" + dummyList);
                            return dummyList;
                            }

                            }








                            package com.varun.list;

                            import java.util.ArrayList;
                            import java.util.Iterator;
                            import java.util.List;

                            public class ArrayListTest {

                            public static void main(String args) {

                            //Create array list
                            List < String > names = new ArrayList < String > ();

                            //Create few names
                            String sam = "Sam";
                            String jamie = "Jamie_dogLover";
                            String bucy = "The Bucky";

                            // add all names in the list
                            names.add(sam);
                            names.add(jamie);
                            names.add(bucy);
                            System.out.println("List values :" + names); //Output : List values :[Sam, Jamie_dogLover, The Bucky]

                            //Update the second index that is -> The Bucky, because list index starts from 0
                            names.set(2, "New Bucky");
                            System.out.println("New List values :" + names); //Output : New List values :[Sam, Jamie_dogLover, New Bucky]

                            //Lest say you want to remove the the 1st index that is jamie
                            names.remove(jamie);
                            System.out.println("Updates List values :" + names); //Output : Updates List values :[Sam, New Bucky]

                            //Let say you get a list of name from somewhere and you want to add into you existing list(names)
                            names.addAll(listOfDummyNames());

                            System.out.println("Updates List values with dummy names :" + names);
                            //Output : Updates List values with dummy names :[Sam, New Bucky, Name0, Name1, Name2, Name3, Name4]
                            //Let say you want to to remove all the name who has "name" String in it

                            Iterator < String > itr = names.iterator();
                            while (itr.hasNext()) {
                            String name = itr.next();
                            if (name.contains("Name")) {
                            itr.remove();
                            }
                            }

                            System.out.println("List after removing all name who has Name in it : " + names);

                            }

                            private static List < String > listOfDummyNames() {

                            List < String > dummyList = new ArrayList < String > ();
                            String name = "Name";
                            for (int i = 0; i < 5; i++) {
                            dummyList.add(name + i);
                            }

                            System.out.println("List of dummy Names :" + dummyList);
                            return dummyList;
                            }

                            }





                            package com.varun.list;

                            import java.util.ArrayList;
                            import java.util.Iterator;
                            import java.util.List;

                            public class ArrayListTest {

                            public static void main(String args) {

                            //Create array list
                            List < String > names = new ArrayList < String > ();

                            //Create few names
                            String sam = "Sam";
                            String jamie = "Jamie_dogLover";
                            String bucy = "The Bucky";

                            // add all names in the list
                            names.add(sam);
                            names.add(jamie);
                            names.add(bucy);
                            System.out.println("List values :" + names); //Output : List values :[Sam, Jamie_dogLover, The Bucky]

                            //Update the second index that is -> The Bucky, because list index starts from 0
                            names.set(2, "New Bucky");
                            System.out.println("New List values :" + names); //Output : New List values :[Sam, Jamie_dogLover, New Bucky]

                            //Lest say you want to remove the the 1st index that is jamie
                            names.remove(jamie);
                            System.out.println("Updates List values :" + names); //Output : Updates List values :[Sam, New Bucky]

                            //Let say you get a list of name from somewhere and you want to add into you existing list(names)
                            names.addAll(listOfDummyNames());

                            System.out.println("Updates List values with dummy names :" + names);
                            //Output : Updates List values with dummy names :[Sam, New Bucky, Name0, Name1, Name2, Name3, Name4]
                            //Let say you want to to remove all the name who has "name" String in it

                            Iterator < String > itr = names.iterator();
                            while (itr.hasNext()) {
                            String name = itr.next();
                            if (name.contains("Name")) {
                            itr.remove();
                            }
                            }

                            System.out.println("List after removing all name who has Name in it : " + names);

                            }

                            private static List < String > listOfDummyNames() {

                            List < String > dummyList = new ArrayList < String > ();
                            String name = "Name";
                            for (int i = 0; i < 5; i++) {
                            dummyList.add(name + i);
                            }

                            System.out.println("List of dummy Names :" + dummyList);
                            return dummyList;
                            }

                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 23 '18 at 9:12









                            VarunVarun

                            3,10385391




                            3,10385391






























                                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%2f53442735%2fa-list-of-strings-in-java%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

                                How to change which sound is reproduced for terminal bell?

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                Can I use Tabulator js library in my java Spring + Thymeleaf project?