{"id":1355,"date":"2022-03-03T07:23:58","date_gmt":"2022-03-03T07:23:58","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1355"},"modified":"2022-03-03T07:23:58","modified_gmt":"2022-03-03T07:23:58","slug":"concatenate-two-arrays","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/concatenate-two-arrays\/","title":{"rendered":"Concatenate Two Arrays"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">&nbsp;Concatenate Two Arrays using arraycopy<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Arrays;\n\npublic class Concat {\n\n    public static void main(String&#91;] args) {\n        int&#91;] array1 = {1, 2, 3};\n        int&#91;] array2 = {4, 5, 6};\n\n        int aLen = array1.length;\n        int bLen = array2.length;\n        int&#91;] result = new int&#91;aLen + bLen];\n\n        System.arraycopy(array1, 0, result, 0, aLen);\n        System.arraycopy(array2, 0, result, aLen, bLen);\n\n        System.out.println(Arrays.toString(result));\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>&#91;1, 2, 3, 4, 5, 6]<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we&#8217;ve two integer arrays&nbsp;<var>array1<\/var>&nbsp;and&nbsp;<var>array2<\/var>.<\/p>\n\n\n\n<p>In order to combine (concatenate) two arrays, we find its length stored in&nbsp;<var>aLen<\/var>&nbsp;and&nbsp;<var>bLen<\/var>&nbsp;respectively. Then, we create a new integer array result with length&nbsp;<var>aLen + bLen<\/var>.<\/p>\n\n\n\n<p>Now, in order to combine both, we copy each element in both arrays to result by using&nbsp;<code>arraycopy()<\/code>&nbsp;function.<\/p>\n\n\n\n<p>The&nbsp;<code>arraycopy(array1, 0, result, 0, aLen)<\/code>&nbsp;function, in simple terms, tells the program to copy&nbsp;<var>array1<\/var>&nbsp;starting from index&nbsp;<code>0<\/code>&nbsp;to&nbsp;<var>result<\/var>&nbsp;from index&nbsp;<code>0<\/code>&nbsp;to&nbsp;<var>aLen<\/var>.<\/p>\n\n\n\n<p>Likewise, for&nbsp;<code>arraycopy(array2, 0, result, aLen, bLen)<\/code>&nbsp;tells the program to copy&nbsp;<var>array2<\/var>&nbsp;starting from index&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>result<\/code>&nbsp;from index&nbsp;<var>aLen<\/var>&nbsp;to&nbsp;<var>bLen<\/var>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;Concatenate Two Arrays using arraycopy Output In the above program, we&#8217;ve two integer arrays&nbsp;array1&nbsp;and&nbsp;array2. In order to combine (concatenate) two arrays, we find its length stored in&nbsp;aLen&nbsp;and&nbsp;bLen&nbsp;respectively. Then, we create a new integer array result with length&nbsp;aLen + bLen. Now, in order to combine both, we copy each element in both arrays to result by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[403],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1355"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1355"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1355\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}