{"id":209,"date":"2014-08-22T11:17:26","date_gmt":"2014-08-22T11:17:26","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=209"},"modified":"2014-08-22T11:17:26","modified_gmt":"2014-08-22T11:17:26","slug":"array-and-arraylist","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=209","title":{"rendered":"Array and Arraylist"},"content":{"rendered":"<p><b>&#8211;Array<\/b><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> class is not part of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections(v=vs.100).aspx\">System.Collections<\/a> namespaces. However, it is still a collection because it is based on the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ilist(v=vs.100).aspx\">IList<\/a> interface.<\/p>\n<p>The rank of an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> object is the number of dimensions in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a>. An <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> can have one or more ranks.<\/p>\n<p><b>The lower bound of an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> is the index of its first element. An <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> can have any lower bound. <\/b>It has a lower bound of zero by default, but a different lower bound can be defined when creating an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> class using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array.createinstance(v=vs.100).aspx\">CreateInstance<\/a>.<\/p>\n<p>Unlike the classes in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections(v=vs.100).aspx\">System.Collections<\/a> namespaces, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> has a fixed capacity. To increase the capacity, you must create a new <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> object with the required capacity, copy the elements from the old <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> object to the new one, and delete the old <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a>.<\/p>\n<p>However, only the system and compilers can derive explicitly from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.100).aspx\">Array<\/a> class. Users should use the array constructs provided by the language that they use.<\/p>\n<p><b>&#8211;Arraylist<\/b><\/p>\n<p>The ArrayList is not guaranteed to be sorted. You must sort the ArrayList prior to performing operations (such as <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist.binarysearch(v=vs.100).aspx\">BinarySearch<\/a>) that require the ArrayList to be sorted.<\/p>\n<p>The capacity of an ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist.trimtosize(v=vs.100).aspx\">TrimToSize<\/a> or by setting the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist.capacity(v=vs.100).aspx\">Capacity<\/a> property explicitly.<\/p>\n<p>Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.<\/p>\n<p>The ArrayList collection accepts null as a valid value, allows duplicate elements.<\/p>\n<p>Using multidimensional arrays as elements in an ArrayList collection is not supported.<\/p>\n<p><b>&#8212;Array and arraylist<\/b><\/p>\n<p>A <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">System.Collections.ArrayList<\/a> or <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">System.Collections.Generic.List&lt;T&gt;<\/a> object is a sophisticated version of an array. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> class and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> generic class provide some features that are offered in most <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections(v=vs.110).aspx\">System.Collections<\/a> classes but that are not in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a> class. For example:<\/p>\n<ul>\n<li>The <b>capacity<\/b> of an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a> is fixed, whereas the capacity of an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> or a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> is automatically expanded as required. If the value of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist.capacity(v=vs.110).aspx\">ArrayList.Capacity<\/a> property is changed, the memory reallocation and copying of elements are automatically done.<\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> provide methods that add, insert, or remove a range of elements. In <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a>, we can <b>get or set the value of only one element at a time<\/b>.<\/li>\n<li>A synchronized version of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> is easy to create by using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist.synchronized(v=vs.110).aspx\">Synchronized<\/a> method; however, this type of synchronization is relatively inefficient. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> classes leaves it up to the user to implement synchronization. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.concurrent(v=vs.110).aspx\">System.Collections.Concurrent<\/a> namespace does not provide a concurrent list type, but it does provide a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd267265(v=vs.110).aspx\">ConcurrentQueue&lt;T&gt;<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd267331(v=vs.110).aspx\">ConcurrentStack&lt;T&gt;<\/a> type.<\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> provide methods that return read-only and fixed-size wrappers to the collection. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a> does not.<\/li>\n<\/ul>\n<p>On the other hand, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a> offers some flexibility that <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> do not. For example:<\/p>\n<ul>\n<li>You can set the lower bound of an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a>, <b>but the lower bound of an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> or a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> is always zero<\/b>.<\/li>\n<li>An <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a> can have multiple dimensions, but an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> or a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> always has exactly one dimension. However, you can easily create a list of arrays or a list of lists.<\/li>\n<li>An <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a> of a specific type (other than <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object(v=vs.110).aspx\">Object<\/a>) provides better performance than an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a>. This is because the elements of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> are of type <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object(v=vs.110).aspx\">Object<\/a>; therefore, boxing and unboxing typically occur when you store or retrieve a value type. However, a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> can provide performance similar to an array of the same type if no reallocations are required; that is, if the initial capacity is a good approximation of the maximum size of the list.<\/li>\n<\/ul>\n<p>Most situations that call for an array can use an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> or a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> instead; they are easier to use and, in general, have performance similar to an array of the same type.<\/p>\n<p><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.array(v=vs.110).aspx\">Array<\/a> is in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system(v=vs.110).aspx\">System<\/a> namespace; <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">ArrayList<\/a> is in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections(v=vs.110).aspx\">System.Collections<\/a> namespace; <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\">List&lt;T&gt;<\/a> is in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.generic(v=vs.110).aspx\">System.Collections.Generic<\/a> namespace.<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">&#8211;Array The Array class is not part of the System.Collections namespaces. However, it is still a collection because it is based on the IList interface. The rank of an Array object is the number of dimensions in the Array. An Array can have one or more ranks. The lower bound of an Array is the index of its first element.&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=209\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=209"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}