{"id":214,"date":"2011-05-31T18:38:19","date_gmt":"2011-05-31T18:38:19","guid":{"rendered":"http:\/\/devnot.wordpress.com\/?p=214"},"modified":"2011-05-31T18:38:19","modified_gmt":"2011-05-31T18:38:19","slug":"remove-or-insert-items-in-an-fixed-array-like-int","status":"publish","type":"post","link":"https:\/\/thomas-jansen.eu\/?p=214","title":{"rendered":"Remove or insert items in an &#8216;fixed&#8217; array like int[]"},"content":{"rendered":"<p>Remove or insert an item in an array.<\/p>\n<p>Use the always useful <em><span style=\"text-decoration:underline;\">Array<\/span><\/em> class. It has some static members which are great!<\/p>\n<pre>int[] myArray = new int[4];\nmyArray [0] = 0;\nmyArray [1] = 1;\nmyArray [2] = 2;\nmyArray [3] = 3;\n\n\/\/ Remove item 1\nArray.Copy(myArray , 2, myArray , 1, 2);\n\/\/ MyArray contains 0,2,3,3\n\nmyArray [0] = 0;\nmyArray [1] = 1;\nmyArray [2] = 2;\nmyArray [3] = 3;\n\n\/\/ Insert new item in 1\nArray.Copy(myArray , 1, myArray ,2, 2);\n\/\/ MyArray contains 0,1,1,2<\/pre>\n<p><strong>Note:<\/strong> If the bounds of the array are exceeded an exception will be thrown.<\/p>\n<p>The functions below calculate the number of elements to copy.<\/p>\n<p>They use the following arguments:<\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td><em>Array<\/em><\/td>\n<td>The array to use.<\/td>\n<\/tr>\n<tr>\n<td><em>index<\/em><\/td>\n<td>The offset whereto insert or remove<\/td>\n<\/tr>\n<tr>\n<td><em>delta<\/em><\/td>\n<td>The number of items to add \/ remove<\/td>\n<\/tr>\n<tr>\n<td><em>totCount<\/em><\/td>\n<td>The number of items currently in the array.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>To allow a correct insert the array must be larger than the current numbers (Arrays are not resized). Clearing is optional<\/p>\n<pre>void removeFromArray(Array array, int index, int delta, int totCount)\n{\n   Array.Copy(array, index + delta, array, index, totCount - (index + delta));\n   Array.Clear(array, totCount - delta, delta);\n}\n\nvoid insertIntoArray(Array array, int index, int delta, int totCount)\n{\n   Array.Copy(array, index, array, index + delta, totCount - index);\n   Array.Clear(array, index, delta);\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remove or insert an item in an array. Use the always useful Array class. It has some static members which are great! int[] myArray = new int[4]; myArray [0] = 0; myArray [1] = 1; myArray [2] = 2; myArray [3] = 3; \/\/ Remove item 1 Array.Copy(myArray , 2, myArray , 1, 2); \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[10,37,62],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-c","tag-array","tag-insert-item","tag-remove-item"],"_links":{"self":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=214"}],"version-history":[{"count":0,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/214\/revisions"}],"wp:attachment":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}