edit-article
Home
Up
Delete
Article Name:
Article Description:
the Array data structure
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">Arrays</h1> <h2>[WHAT]</h2> <ol> <li><strong>] Array stores a number of elements in a specific order. Here are some examples</strong></li> <ol> <li>] pets = ["dog","cat","mouse"]</li> <li>] numbersOdd = [1,3,5,7,9]</li> <li>] datesHolidays2013 = ['2013-01-01','2013-04-21','2013-07-01']</li> <li>] namesHolidays = ['New Years Day','Good Friday', 'Canada Day'] </li> </ol> <li><strong>] They are accessed using an integer to specify which element is required (although the elements may be of almost any type).</strong></li> <ol> <li>] var myPet = pets[0], where 0 is the first element in the pets Array</li> </ol> <li><strong>] Typical implementations allocate contiguous memory words for the elements of arrays (but this is not always a necessity).</strong></li> <ol> <li>] how arrays are stored in memory, a sequential block of</li> </ol> <li><strong>] Arrays may be fixed-length or expandable.</strong></li> <ol> <li>]</li> </ol> <li><strong>] single dimensional array</strong></li> <ol> <li>] the array contains a number of items(), all of the same type</li> </ol> <li><strong>] multi-dimensional arrays</strong></li> <ol> <li>]</li> </ol></ol> <h2>[WHY]</h2> <ol> <li><strong>] define ...</strong></li> <ol> <li>] a collection of contiguous elements, may be a fixed size or expandable(dynamic), each element is accessed by its numeric index </li> <li>] a set/collection of related data elements accessed by their index</li> <li>] stores a number of elements in a specific order. They are accessed using an integer (index) to specify which element is required (although the elements may be of almost any type). Typical implementations allocate contiguous memory words for the elements of arrays (but this is not always a necessity). Arrays may be fixed-length or expandable.</li> </ol> <li><strong>] operations on array </strong><ol> <li>] Allocation</li> <li>] Accessing</li> <li>] Redimensioning</li> </ol></li> </ol> <h2>[WHERE]</h2> <ol> <li>]</li> </ol> <h2>[WHEN]</h2> <ol> <li>]</li> </ol> <h2>[EXAMPLE]</h2> <h2>[HOW-TO example code, language = javascript]</h2> <ol> <li><strong>] CREATE / DECLARATION</strong></li> <ol> <li>] var pets = new Array("dog","cat","mouse") || var pets = ["dog","cat","mouse"];</li> </ol> <li><strong>] ACCESS</strong></li> <ol> <li>] return pets[1]; // output = cat</li> </ol> <li><strong>] ADD ELEMENT</strong></li> <ol> <li>] pets.push("gerbil"); - adds a new element to the end of the array, returns the new length of the array</li> </ol> <li><strong>] REMOVE ELEMENT</strong></li> <ol> <li>] pets.pop() - removes the last element of the array,</li> </ol> <li><strong>] REPLACE ELEMENT</strong></li> <ol> <li>] pets[0] = "pup";</li> </ol> <li><strong>] MORE Methods and Examples in</strong></li> <ol> <li>] javascript arrays</li> </ol></ol> <h2>[REFERENCE]</h2> <ol> <li>]</li> </ol> <h1 style="text-align: center;"> </h1>