{"id":115432,"date":"2020-03-24T12:25:36","date_gmt":"2020-03-24T12:25:36","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=115432"},"modified":"2025-04-01T08:28:53","modified_gmt":"2025-04-01T08:28:53","slug":"add-elements-to-array-java","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/add-elements-to-array-java\/","title":{"rendered":"How To Add Elements To An Array In Java"},"content":{"rendered":"<p><strong>This Tutorial Discusses Various Methods to add Elements to the Array in Java. Some Options are to Use a New Array, to use an ArrayList, etc.:<\/strong><\/p>\n<p>The arrays in Java are of fixed size i.e. once declared you cannot change their size. So when there is a requirement to add a new element to the array, you can follow any of the approaches given below.<\/p>\n<ul>\n<li>Using a new array larger than the original to add a new element.<\/li>\n<li>Using ArrayList as an intermediate structure.<\/li>\n<li>Shifting the elements to accommodate the new element.<\/li>\n<\/ul>\n<p><strong>=&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/java\/\">Visit Here To See The Java Training Series For All.<\/a><\/strong><\/p>\n<p><em><strong> <\/strong><\/em><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Adding-an-element-to-an-Array.png\"><img decoding=\"async\" class=\"alignnone wp-image-115472 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Adding-an-element-to-an-Array.png\" alt=\"Add an element to an Array in Java\" width=\"650\" height=\"366\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Adding-an-element-to-an-Array.png 650w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Adding-an-element-to-an-Array-300x169.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><\/p>\n<h2>Java Add To Array &#8211; Adding Elements To An Array<\/h2>\n<p>In this tutorial, we will discuss all the above three methods for adding an element to the array.<\/p>\n<h3><span style=\"color: #ff6600;\">Use A New Array To Accommodate The Original Array And New Element<\/span><\/h3>\n<p>In this approach, you will create a new array with a size more than the original array. <span style=\"text-decoration: underline;\"><strong>For example,<\/strong><\/span> if the original array size is N, you will create a new array with size N+1 in case you want to add one element.<\/p>\n<p>Once a new array is created, you can copy the original array of N elements into the new array. Then add the new element at (N+1)<sup>th<\/sup> location.<\/p>\n<p><strong>The program to add an element with the above approach is given below.<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.*;\r\nclass Main{ \r\n    \/\/ Function to add x in arr\r\n    public static int&#x5B;] add_element(int n, int myarray&#x5B;], int ele) \r\n    { \r\n         int i; \r\n\r\n         int newArray&#x5B;] = new int&#x5B;n + 1]; \r\n        \/\/copy original array into new array\r\n        for (i = 0; i &amp;lt; n; i++) \r\n              newArray&#x5B;i] = myarray&#x5B;i]; \r\n\r\n        \/\/add element to the new array\r\n        newArray&#x5B;n] = ele; \r\n\r\n        returnnewArray; \r\n    } \r\n\r\n     public static void main(String&#x5B;] args) \r\n    { \r\n        int n = 5; \r\n        int i; \r\n\r\n        \/\/ Original array with size 5 \r\n        int myArray&#x5B;] = { 1, 3, 5, 7, 9 }; \r\n\r\n        System.out.println(&quot;Original Array:\\n&quot;  + Arrays.toString(myArray)); \r\n        \/\/new element to be added to array\r\n        int ele = 11; \r\n\r\n        myArray = add_element(n, myArray, ele); \r\n\r\n        System.out.println(&quot;\\nArray after adding &quot; + ele + &quot;:\\n&quot; + Arrays.toString(myArray)); \r\n    } \r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Use-a-new-array-to-accommodate-the-original-array-and-new-element.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-115447\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Use-a-new-array-to-accommodate-the-original-array-and-new-element.png\" alt=\"Use a new array to accommodate the original array and new element\" width=\"233\" height=\"119\" \/><\/a><\/p>\n<p>In this technique, you simply create a new array larger than the original by one element. You copy all the elements of the original array to the new array and then insert a new element at the end of the new array.<\/p>\n<p>This is a traditional method that is quite slow and not that efficient.<\/p>\n<h3><span style=\"color: #ff6600;\">Use ArrayList As An Intermediate Structure<\/span><\/h3>\n<p>ArrayList is a data structure that is dynamic in nature. Hence you can dynamically increase the size of the array list and add as many elements to it. Thus you can use ArrayList as an intermediate structure while adding elements to the array<\/p>\n<p><strong>For adding an element to the array,<\/strong><\/p>\n<ul>\n<li>First, you can convert array to ArrayList using \u2018asList ()\u2019 method of ArrayList.<\/li>\n<li>Add an element to the ArrayList using the \u2018add\u2019 method.<\/li>\n<li>Convert the ArrayList back to the array using the \u2018toArray()\u2019 method.<\/li>\n<\/ul>\n<p><strong>Let&#8217;s put these steps into an implementation.<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.*; \r\nclass Main {   \r\npublic static void main(String&#x5B;] args)     {  \r\n        \/\/ Original array with size 5\r\n        Integer odd_Array&#x5B;] = { 1,3,5,7,9 }; \r\n\r\n        \/\/ display the original array \r\n        System.out.println(&quot;Original Array:&quot; + Arrays.toString(odd_Array)); \r\n\r\n        \/\/ element to be added \r\n        int val = 11; \r\n\r\n        \/\/ convert array to Arraylist\r\n        List&amp;lt;Integer&amp;gt;oddlist = new ArrayList&amp;lt;Integer&amp;gt;(Arrays.asList(odd_Array)); \r\n\r\n        \/\/ Add the new element \r\n        oddlist.add(val); \r\n\r\n        \/\/ Convert the Arraylist back to array \r\n        odd_Array = oddlist.toArray(odd_Array); \r\n\r\n        \/\/ display the updated array \r\n        System.out.println(&quot;\\nArray after adding element &quot; + val + &quot;:&quot; + Arrays.toString(odd_Array)); \r\n    } \r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Use-ArrayList-as-an-intermediate-structure.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-115448\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Use-ArrayList-as-an-intermediate-structure.png\" alt=\"Use ArrayList as an intermediate structure\" width=\"427\" height=\"54\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Use-ArrayList-as-an-intermediate-structure.png 427w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Use-ArrayList-as-an-intermediate-structure-300x38.png 300w\" sizes=\"(max-width: 427px) 100vw, 427px\" \/><\/a><\/p>\n<p>The above program shows an array of odd numbers. It is converted to ArrayList. Then another odd number is added to this list. Next, the ArrayList is converted back to the array and an updated array is displayed.<\/p>\n<h3><span style=\"color: #ff6600;\">Shifting The Elements To Accommodate The New Element<\/span><\/h3>\n<p>The above two methods of adding an element to the array dealt with elements being added at the end of the array. So these methods were rather easy to implement. But what about the case wherein you need to add an element at a specific position?<\/p>\n<p>In this case, the implementation is a little tough.<\/p>\n<p><strong>Let&#8217;s list the sequence of steps.<\/strong><\/p>\n<ul>\n<li>Create a new destination array with a size more than the original array.<\/li>\n<li>Then copy the elements from the original array before the specified index to the new array.<\/li>\n<li>Shift the elements after the index to the right by one position so that you create a space for the new element.<\/li>\n<li>Insert a new element at the specified index in the destination array.<\/li>\n<\/ul>\n<p><strong>The following program implements this technique.<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimportjava.util.*; \r\nclass Main {   \r\npublic static void main(String&#x5B;] args) \r\n    {   \r\n        \/\/ Original array with size 5\r\n        Integer odd_Array&#x5B;] = { 1,3,7,9,11 }; \r\n\r\n        \/\/ display the original array \r\n        System.out.println(&quot;Original Array:&quot; + Arrays.toString(odd_Array)); \r\n\r\n        \/\/ element to be added at index\r\n        int val = 5; \r\n        int index = 2;\r\n        \/\/dest array with size more than 1 of the original array\r\n        int&#x5B;] dest_Array = new int&#x5B;odd_Array.length+1];\r\n        int j = 0;\r\n        \/\/Iterate dest_array and insert new element as well as shift other elements to the right\r\n       for(int i = 0; i &amp;lt;dest_Array.length; i++) {\r\n\t     if(i == index) {\r\n\t           dest_Array&#x5B;i] = val;\r\n             }\r\n             else {\r\n                   dest_Array&#x5B;i] = odd_Array&#x5B;j];\r\n                   j++;\r\n             }\r\n        }\r\n        \/\/ display the updated array \r\n        System.out.println(&quot;\\nArray after adding element &quot; + val + &quot; at index &quot; + index +&quot;:&quot; \r\n                            + Arrays.toString(dest_Array)); \r\n    } \r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Shifting-the-elements-to-accommodate-the-new-element.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-115449\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Shifting-the-elements-to-accommodate-the-new-element.png\" alt=\"Shifting the elements to accommodate the new element\" width=\"509\" height=\"74\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Shifting-the-elements-to-accommodate-the-new-element.png 509w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Shifting-the-elements-to-accommodate-the-new-element-300x44.png 300w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/><\/a><\/p>\n<p>Here given an array of odd numbers, we need to insert number 5 at position (index) 2 in the array. To do this, we create another destination array with the size as one more than that of the original array. Now over a loop, we shift the original array elements to the new array till we reach the index where the new element is to be added.<\/p>\n<p>We add the new element at index 2 in the new array. Then starting from index 2, we copy all the other elements from the old array to the new array by shifting their indices by 1 to the right.<\/p>\n<h3><span style=\"color: #ff6600;\">Frequently Asked Questions<\/span><\/h3>\n<p><span style=\"color: #ff6600;\"><strong>Q #1) Can we increase the size of the array in Java?<\/strong><\/span><\/p>\n<p><strong>Answer:<\/strong> No. We cannot increase the size of the array in Java once it is instantiated. If at all you need a different size for the array, create a new array and move all the elements to the new array or use an ArrayList which dynamically changes its size.<\/p>\n<p><strong><span style=\"color: #ff6600;\">Q #2) How do you add two arrays in Java?<\/span><\/strong><\/p>\n<p><strong>Answer: <\/strong>You can either add two arrays or form a resultant array manually by using for loop. Or you can use the arrayCopy method to copy one array into another. For both the techniques, create a resultant array with enough room to accommodate both the arrays.<\/p>\n<p><strong><span style=\"color: #ff6600;\">Q #3) How do you add an ArrayList to an Array in Java?<\/span><\/strong><\/p>\n<p><strong>Answer: <\/strong>Create a list of n items. Then use the toArray method of the list to convert it to the array.<\/p>\n<p><strong><span style=\"color: #ff6600;\">Q #4) What is a growable array in Java?<\/span><\/strong><\/p>\n<p><strong>Answer: <\/strong>A growable array is simply a dynamic array which increases its size when more items are added to it. In Java, this is an ArrayList.<\/p>\n<p><strong><span style=\"color: #ff6600;\">Q #5) Can you declare an array without assigning the size of an array?<\/span><\/strong><\/p>\n<p><strong>Answer: <\/strong>No. Array size must be declared before using it. If not, it results in a compilation error.<\/p>\n<p><strong><span style=\"color: #ff6600;\">Q #6) Can you add multiple elements to an Array at once?<\/span><\/strong><\/p>\n<p><strong>Answer: <\/strong>No. You cannot add only one element to an array at a given instant. If you want to add multiple elements to the array at once, you can think of initializing the array with multiple elements or convert the array to ArrayList. ArrayList has an \u2018addAll\u2019 method that can add multiple elements to the ArrayList.<\/p>\n<h2>Conclusion<\/h2>\n<p>Adding a new element to the array can be done using three techniques. The first technique is less efficient wherein we just create a new array with increased size and then copy the elements from earlier array into it and then add the new element.<\/p>\n<p>The most efficient one is using ArrayList to add a new element. We just convert the array to the ArrayList and then add the element to the list. Then we convert the ArrayList back to the array.<\/p>\n<p>These techniques only take care of adding an element at the end of the list. If we want to add an element in between the array at a specified index, then we need to shift the elements after the specified index to the right by one position and then accommodate the new element.<\/p>\n<p>We have seen all these three techniques with examples in this tutorial. We will discuss some more array operations in our subsequent tutorials.<\/p>\n<p><strong>=&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/java\/\">Check ALL Java Tutorials Here.<\/a><\/strong><\/p>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"115432\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>This Tutorial Discusses Various Methods to add Elements to the Array in Java. Some Options are to Use a New Array, to use an ArrayList, etc.: The arrays in Java are of fixed size i.e. once declared you cannot change their size. So when there is a requirement to add &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How To Add Elements To An Array In Java\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/add-elements-to-array-java\/#more-115432\" aria-label=\"Read more about How To Add Elements To An Array In Java\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":115472,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[408],"tags":[],"class_list":{"0":"post-115432","1":"page","2":"type-page","3":"status-publish","4":"has-post-thumbnail","6":"category-java"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/115432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=115432"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/115432\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/115472"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=115432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=115432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=115432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}