Facing an error while assigning values to a list












2















I have a list which has 3 elements. It is in a while loop and I want to overwrite its values in each iterations. Here is the code:



private List<Integer> loadList = new ArrayList<>(3);
.
.
.
while(counter < 10) {
loadList.add(0, taskNum);
loadList.add(1, taskNum);
loadList.add(2, taskNum);
.
.
.
counter++;
}


But after running the code I realized that overwriting is not happening and each time the new elements are being added to the end of the list. For example:




At first : a = [1 2 3]



Then: a = [1 2 3 4 5 6] but I want it to be like a = [4 5 6]




So I changed my code to something like this:



loadList = new ArrayList<>(3);
//initializing
loadList.add(0, 0);
loadList.add(1, 0);
loadList.add(2, 0);
while(counter < 10) {
loadList.set(0, taskNum);
loadList.set(1, taskNum);
loadList.set(2, taskNum);
.
.
.
counter++;
}


But now I'm getting this error:



2018-11-24 13:27:38.298 ERROR [n.f.core.Main] Exception in main
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:657) ~[na:1.8.0_151]
at java.util.ArrayList.set(ArrayList.java:448) ~[na:1.8.0_151]
at net.floodlightcontroller.mactracker.Mactracker.paramInit(Mactracker.java:137) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.mactracker.Mactracker.init(Mactracker.java:205) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.initModules(FloodlightModuleLoader.java:460) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.loadModulesFromList(FloodlightModuleLoader.java:295) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.loadModulesFromConfig(FloodlightModuleLoader.java:235) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.Main.main(Main.java:61) ~[floodlight.jar:1.2-SNAPSHOT]


What's wrong and how can I solve this problem?










share|improve this question

























  • BTW, what is taskNum, how are the values getting set to it?

    – Nicholas K
    Nov 24 '18 at 10:19











  • The value of taskNum is variable and is changing in another part of the code. I thought it is not necessary to mention the way its value is changing.

    – helen
    Nov 24 '18 at 10:34
















2















I have a list which has 3 elements. It is in a while loop and I want to overwrite its values in each iterations. Here is the code:



private List<Integer> loadList = new ArrayList<>(3);
.
.
.
while(counter < 10) {
loadList.add(0, taskNum);
loadList.add(1, taskNum);
loadList.add(2, taskNum);
.
.
.
counter++;
}


But after running the code I realized that overwriting is not happening and each time the new elements are being added to the end of the list. For example:




At first : a = [1 2 3]



Then: a = [1 2 3 4 5 6] but I want it to be like a = [4 5 6]




So I changed my code to something like this:



loadList = new ArrayList<>(3);
//initializing
loadList.add(0, 0);
loadList.add(1, 0);
loadList.add(2, 0);
while(counter < 10) {
loadList.set(0, taskNum);
loadList.set(1, taskNum);
loadList.set(2, taskNum);
.
.
.
counter++;
}


But now I'm getting this error:



2018-11-24 13:27:38.298 ERROR [n.f.core.Main] Exception in main
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:657) ~[na:1.8.0_151]
at java.util.ArrayList.set(ArrayList.java:448) ~[na:1.8.0_151]
at net.floodlightcontroller.mactracker.Mactracker.paramInit(Mactracker.java:137) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.mactracker.Mactracker.init(Mactracker.java:205) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.initModules(FloodlightModuleLoader.java:460) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.loadModulesFromList(FloodlightModuleLoader.java:295) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.loadModulesFromConfig(FloodlightModuleLoader.java:235) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.Main.main(Main.java:61) ~[floodlight.jar:1.2-SNAPSHOT]


What's wrong and how can I solve this problem?










share|improve this question

























  • BTW, what is taskNum, how are the values getting set to it?

    – Nicholas K
    Nov 24 '18 at 10:19











  • The value of taskNum is variable and is changing in another part of the code. I thought it is not necessary to mention the way its value is changing.

    – helen
    Nov 24 '18 at 10:34














2












2








2








I have a list which has 3 elements. It is in a while loop and I want to overwrite its values in each iterations. Here is the code:



private List<Integer> loadList = new ArrayList<>(3);
.
.
.
while(counter < 10) {
loadList.add(0, taskNum);
loadList.add(1, taskNum);
loadList.add(2, taskNum);
.
.
.
counter++;
}


But after running the code I realized that overwriting is not happening and each time the new elements are being added to the end of the list. For example:




At first : a = [1 2 3]



Then: a = [1 2 3 4 5 6] but I want it to be like a = [4 5 6]




So I changed my code to something like this:



loadList = new ArrayList<>(3);
//initializing
loadList.add(0, 0);
loadList.add(1, 0);
loadList.add(2, 0);
while(counter < 10) {
loadList.set(0, taskNum);
loadList.set(1, taskNum);
loadList.set(2, taskNum);
.
.
.
counter++;
}


But now I'm getting this error:



2018-11-24 13:27:38.298 ERROR [n.f.core.Main] Exception in main
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:657) ~[na:1.8.0_151]
at java.util.ArrayList.set(ArrayList.java:448) ~[na:1.8.0_151]
at net.floodlightcontroller.mactracker.Mactracker.paramInit(Mactracker.java:137) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.mactracker.Mactracker.init(Mactracker.java:205) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.initModules(FloodlightModuleLoader.java:460) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.loadModulesFromList(FloodlightModuleLoader.java:295) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.loadModulesFromConfig(FloodlightModuleLoader.java:235) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.Main.main(Main.java:61) ~[floodlight.jar:1.2-SNAPSHOT]


What's wrong and how can I solve this problem?










share|improve this question
















I have a list which has 3 elements. It is in a while loop and I want to overwrite its values in each iterations. Here is the code:



private List<Integer> loadList = new ArrayList<>(3);
.
.
.
while(counter < 10) {
loadList.add(0, taskNum);
loadList.add(1, taskNum);
loadList.add(2, taskNum);
.
.
.
counter++;
}


But after running the code I realized that overwriting is not happening and each time the new elements are being added to the end of the list. For example:




At first : a = [1 2 3]



Then: a = [1 2 3 4 5 6] but I want it to be like a = [4 5 6]




So I changed my code to something like this:



loadList = new ArrayList<>(3);
//initializing
loadList.add(0, 0);
loadList.add(1, 0);
loadList.add(2, 0);
while(counter < 10) {
loadList.set(0, taskNum);
loadList.set(1, taskNum);
loadList.set(2, taskNum);
.
.
.
counter++;
}


But now I'm getting this error:



2018-11-24 13:27:38.298 ERROR [n.f.core.Main] Exception in main
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:657) ~[na:1.8.0_151]
at java.util.ArrayList.set(ArrayList.java:448) ~[na:1.8.0_151]
at net.floodlightcontroller.mactracker.Mactracker.paramInit(Mactracker.java:137) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.mactracker.Mactracker.init(Mactracker.java:205) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.initModules(FloodlightModuleLoader.java:460) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.loadModulesFromList(FloodlightModuleLoader.java:295) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.module.FloodlightModuleLoader.loadModulesFromConfig(FloodlightModuleLoader.java:235) ~[floodlight.jar:1.2-SNAPSHOT]
at net.floodlightcontroller.core.Main.main(Main.java:61) ~[floodlight.jar:1.2-SNAPSHOT]


What's wrong and how can I solve this problem?







java list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 10:27







helen

















asked Nov 24 '18 at 10:12









helenhelen

302217




302217













  • BTW, what is taskNum, how are the values getting set to it?

    – Nicholas K
    Nov 24 '18 at 10:19











  • The value of taskNum is variable and is changing in another part of the code. I thought it is not necessary to mention the way its value is changing.

    – helen
    Nov 24 '18 at 10:34



















  • BTW, what is taskNum, how are the values getting set to it?

    – Nicholas K
    Nov 24 '18 at 10:19











  • The value of taskNum is variable and is changing in another part of the code. I thought it is not necessary to mention the way its value is changing.

    – helen
    Nov 24 '18 at 10:34

















BTW, what is taskNum, how are the values getting set to it?

– Nicholas K
Nov 24 '18 at 10:19





BTW, what is taskNum, how are the values getting set to it?

– Nicholas K
Nov 24 '18 at 10:19













The value of taskNum is variable and is changing in another part of the code. I thought it is not necessary to mention the way its value is changing.

– helen
Nov 24 '18 at 10:34





The value of taskNum is variable and is changing in another part of the code. I thought it is not necessary to mention the way its value is changing.

– helen
Nov 24 '18 at 10:34












5 Answers
5






active

oldest

votes


















5














With this line:



loadList = new ArrayList<>(3);


you defined that the initial capacity of the list is 3.

Now you can add as many items as you want not only 3.

But still your list is empty.

You cannot use:



loadList.set(2, taskNum);


if there is no item in the 3d position.
set(position, item) is valid only if there is already an item at position.

So use add(item) to add new items at the end of the list

and set(position, item) to replace the item that is already in the list at position.

You can also use add(position, item) to insert a new item at position, and shift the items from this position to the right.






share|improve this answer

































    2














    You are getting the IndexOutOfBoundsException as you are trying to set value at an index which is greater than the size of the list.



    loadList.add(0, 0);
    loadList.add(1, 0);
    loadList.set(2, 0); <--------------- You will get IndexOutOfBound here


    As List#set method throws an exception when if the index is out of range(index < 0 || index > size(). Use loadList.add(2, 0), instead of loadList.set(2, 0); will solve your issue.






    share|improve this answer





















    • 2





      In other words, that last set should also have been an add, just like the first two...

      – Kevin Anderson
      Nov 24 '18 at 10:22



















    1














    When you initializing like this,



    loadList.add(0, 0);
    loadList.add(1, 0);
    loadList.set(2, 0); <--- here you are setting instead of adding the value :)


    It should be changed to,



    loadList.add(0, 0);
    loadList.add(1, 0);
    loadList.add(2, 0);





    share|improve this answer































      1














      you are getting IndexOutOfBoundsException because you set the object at the particular index which is not defined yet.



      loadList = new ArrayList<>(3); menas your arryList have initial capacity 3.


      first you have to add element at positions then set the elements. try this



      loadList.add(0, 0);
      loadList.add(1, 0);
      loadList.add(2, 0);





      share|improve this answer































        1














        An Example, That will give you an Idea.



        List alist = new ArrayList();
        alist.add("1");
        alist.add("2");
        alist.add("3");
        int j=4;
        for(int i=0;i<=2;i++){
        System.out.println("alist.get(0)"+alist.get(0));
        System.out.println("alist.get(1)"+alist.get(1));
        System.out.println("alist.get(2)"+alist.get(2)+"n");
        alist.add(0, j++);
        alist.add(1, j++);
        alist.add(2, j++);
        }





        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%2f53457125%2ffacing-an-error-while-assigning-values-to-a-list%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









          5














          With this line:



          loadList = new ArrayList<>(3);


          you defined that the initial capacity of the list is 3.

          Now you can add as many items as you want not only 3.

          But still your list is empty.

          You cannot use:



          loadList.set(2, taskNum);


          if there is no item in the 3d position.
          set(position, item) is valid only if there is already an item at position.

          So use add(item) to add new items at the end of the list

          and set(position, item) to replace the item that is already in the list at position.

          You can also use add(position, item) to insert a new item at position, and shift the items from this position to the right.






          share|improve this answer






























            5














            With this line:



            loadList = new ArrayList<>(3);


            you defined that the initial capacity of the list is 3.

            Now you can add as many items as you want not only 3.

            But still your list is empty.

            You cannot use:



            loadList.set(2, taskNum);


            if there is no item in the 3d position.
            set(position, item) is valid only if there is already an item at position.

            So use add(item) to add new items at the end of the list

            and set(position, item) to replace the item that is already in the list at position.

            You can also use add(position, item) to insert a new item at position, and shift the items from this position to the right.






            share|improve this answer




























              5












              5








              5







              With this line:



              loadList = new ArrayList<>(3);


              you defined that the initial capacity of the list is 3.

              Now you can add as many items as you want not only 3.

              But still your list is empty.

              You cannot use:



              loadList.set(2, taskNum);


              if there is no item in the 3d position.
              set(position, item) is valid only if there is already an item at position.

              So use add(item) to add new items at the end of the list

              and set(position, item) to replace the item that is already in the list at position.

              You can also use add(position, item) to insert a new item at position, and shift the items from this position to the right.






              share|improve this answer















              With this line:



              loadList = new ArrayList<>(3);


              you defined that the initial capacity of the list is 3.

              Now you can add as many items as you want not only 3.

              But still your list is empty.

              You cannot use:



              loadList.set(2, taskNum);


              if there is no item in the 3d position.
              set(position, item) is valid only if there is already an item at position.

              So use add(item) to add new items at the end of the list

              and set(position, item) to replace the item that is already in the list at position.

              You can also use add(position, item) to insert a new item at position, and shift the items from this position to the right.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 24 '18 at 10:27

























              answered Nov 24 '18 at 10:20









              forpasforpas

              14.5k3625




              14.5k3625

























                  2














                  You are getting the IndexOutOfBoundsException as you are trying to set value at an index which is greater than the size of the list.



                  loadList.add(0, 0);
                  loadList.add(1, 0);
                  loadList.set(2, 0); <--------------- You will get IndexOutOfBound here


                  As List#set method throws an exception when if the index is out of range(index < 0 || index > size(). Use loadList.add(2, 0), instead of loadList.set(2, 0); will solve your issue.






                  share|improve this answer





















                  • 2





                    In other words, that last set should also have been an add, just like the first two...

                    – Kevin Anderson
                    Nov 24 '18 at 10:22
















                  2














                  You are getting the IndexOutOfBoundsException as you are trying to set value at an index which is greater than the size of the list.



                  loadList.add(0, 0);
                  loadList.add(1, 0);
                  loadList.set(2, 0); <--------------- You will get IndexOutOfBound here


                  As List#set method throws an exception when if the index is out of range(index < 0 || index > size(). Use loadList.add(2, 0), instead of loadList.set(2, 0); will solve your issue.






                  share|improve this answer





















                  • 2





                    In other words, that last set should also have been an add, just like the first two...

                    – Kevin Anderson
                    Nov 24 '18 at 10:22














                  2












                  2








                  2







                  You are getting the IndexOutOfBoundsException as you are trying to set value at an index which is greater than the size of the list.



                  loadList.add(0, 0);
                  loadList.add(1, 0);
                  loadList.set(2, 0); <--------------- You will get IndexOutOfBound here


                  As List#set method throws an exception when if the index is out of range(index < 0 || index > size(). Use loadList.add(2, 0), instead of loadList.set(2, 0); will solve your issue.






                  share|improve this answer















                  You are getting the IndexOutOfBoundsException as you are trying to set value at an index which is greater than the size of the list.



                  loadList.add(0, 0);
                  loadList.add(1, 0);
                  loadList.set(2, 0); <--------------- You will get IndexOutOfBound here


                  As List#set method throws an exception when if the index is out of range(index < 0 || index > size(). Use loadList.add(2, 0), instead of loadList.set(2, 0); will solve your issue.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 10:23

























                  answered Nov 24 '18 at 10:19









                  Amit BeraAmit Bera

                  3,8051628




                  3,8051628








                  • 2





                    In other words, that last set should also have been an add, just like the first two...

                    – Kevin Anderson
                    Nov 24 '18 at 10:22














                  • 2





                    In other words, that last set should also have been an add, just like the first two...

                    – Kevin Anderson
                    Nov 24 '18 at 10:22








                  2




                  2





                  In other words, that last set should also have been an add, just like the first two...

                  – Kevin Anderson
                  Nov 24 '18 at 10:22





                  In other words, that last set should also have been an add, just like the first two...

                  – Kevin Anderson
                  Nov 24 '18 at 10:22











                  1














                  When you initializing like this,



                  loadList.add(0, 0);
                  loadList.add(1, 0);
                  loadList.set(2, 0); <--- here you are setting instead of adding the value :)


                  It should be changed to,



                  loadList.add(0, 0);
                  loadList.add(1, 0);
                  loadList.add(2, 0);





                  share|improve this answer




























                    1














                    When you initializing like this,



                    loadList.add(0, 0);
                    loadList.add(1, 0);
                    loadList.set(2, 0); <--- here you are setting instead of adding the value :)


                    It should be changed to,



                    loadList.add(0, 0);
                    loadList.add(1, 0);
                    loadList.add(2, 0);





                    share|improve this answer


























                      1












                      1








                      1







                      When you initializing like this,



                      loadList.add(0, 0);
                      loadList.add(1, 0);
                      loadList.set(2, 0); <--- here you are setting instead of adding the value :)


                      It should be changed to,



                      loadList.add(0, 0);
                      loadList.add(1, 0);
                      loadList.add(2, 0);





                      share|improve this answer













                      When you initializing like this,



                      loadList.add(0, 0);
                      loadList.add(1, 0);
                      loadList.set(2, 0); <--- here you are setting instead of adding the value :)


                      It should be changed to,



                      loadList.add(0, 0);
                      loadList.add(1, 0);
                      loadList.add(2, 0);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 24 '18 at 10:21









                      SandSand

                      1,7212721




                      1,7212721























                          1














                          you are getting IndexOutOfBoundsException because you set the object at the particular index which is not defined yet.



                          loadList = new ArrayList<>(3); menas your arryList have initial capacity 3.


                          first you have to add element at positions then set the elements. try this



                          loadList.add(0, 0);
                          loadList.add(1, 0);
                          loadList.add(2, 0);





                          share|improve this answer




























                            1














                            you are getting IndexOutOfBoundsException because you set the object at the particular index which is not defined yet.



                            loadList = new ArrayList<>(3); menas your arryList have initial capacity 3.


                            first you have to add element at positions then set the elements. try this



                            loadList.add(0, 0);
                            loadList.add(1, 0);
                            loadList.add(2, 0);





                            share|improve this answer


























                              1












                              1








                              1







                              you are getting IndexOutOfBoundsException because you set the object at the particular index which is not defined yet.



                              loadList = new ArrayList<>(3); menas your arryList have initial capacity 3.


                              first you have to add element at positions then set the elements. try this



                              loadList.add(0, 0);
                              loadList.add(1, 0);
                              loadList.add(2, 0);





                              share|improve this answer













                              you are getting IndexOutOfBoundsException because you set the object at the particular index which is not defined yet.



                              loadList = new ArrayList<>(3); menas your arryList have initial capacity 3.


                              first you have to add element at positions then set the elements. try this



                              loadList.add(0, 0);
                              loadList.add(1, 0);
                              loadList.add(2, 0);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 24 '18 at 10:23









                              Khalid ShahKhalid Shah

                              1,3951820




                              1,3951820























                                  1














                                  An Example, That will give you an Idea.



                                  List alist = new ArrayList();
                                  alist.add("1");
                                  alist.add("2");
                                  alist.add("3");
                                  int j=4;
                                  for(int i=0;i<=2;i++){
                                  System.out.println("alist.get(0)"+alist.get(0));
                                  System.out.println("alist.get(1)"+alist.get(1));
                                  System.out.println("alist.get(2)"+alist.get(2)+"n");
                                  alist.add(0, j++);
                                  alist.add(1, j++);
                                  alist.add(2, j++);
                                  }





                                  share|improve this answer




























                                    1














                                    An Example, That will give you an Idea.



                                    List alist = new ArrayList();
                                    alist.add("1");
                                    alist.add("2");
                                    alist.add("3");
                                    int j=4;
                                    for(int i=0;i<=2;i++){
                                    System.out.println("alist.get(0)"+alist.get(0));
                                    System.out.println("alist.get(1)"+alist.get(1));
                                    System.out.println("alist.get(2)"+alist.get(2)+"n");
                                    alist.add(0, j++);
                                    alist.add(1, j++);
                                    alist.add(2, j++);
                                    }





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      An Example, That will give you an Idea.



                                      List alist = new ArrayList();
                                      alist.add("1");
                                      alist.add("2");
                                      alist.add("3");
                                      int j=4;
                                      for(int i=0;i<=2;i++){
                                      System.out.println("alist.get(0)"+alist.get(0));
                                      System.out.println("alist.get(1)"+alist.get(1));
                                      System.out.println("alist.get(2)"+alist.get(2)+"n");
                                      alist.add(0, j++);
                                      alist.add(1, j++);
                                      alist.add(2, j++);
                                      }





                                      share|improve this answer













                                      An Example, That will give you an Idea.



                                      List alist = new ArrayList();
                                      alist.add("1");
                                      alist.add("2");
                                      alist.add("3");
                                      int j=4;
                                      for(int i=0;i<=2;i++){
                                      System.out.println("alist.get(0)"+alist.get(0));
                                      System.out.println("alist.get(1)"+alist.get(1));
                                      System.out.println("alist.get(2)"+alist.get(2)+"n");
                                      alist.add(0, j++);
                                      alist.add(1, j++);
                                      alist.add(2, j++);
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 24 '18 at 11:07









                                      Kumar Gaurav SharmaKumar Gaurav Sharma

                                      69112




                                      69112






























                                          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%2f53457125%2ffacing-an-error-while-assigning-values-to-a-list%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

                                          Create new schema in PostgreSQL using DBeaver

                                          Deepest pit of an array with Javascript: test on Codility

                                          Costa Masnaga