Array
Array
Array is a data structure consisting of a collection of elements, each identified by at least one array index or key. The index starts from 0 (0-based numbering). Insertion, removal and search operations in arrays are fast because of random access. Given an index of an array, the value can be retrieved directly from the memory.
Multidimensional Arrays
Arrays are very handle when dealing with mathematical operations. A matrix can be constructed with a two-dimensional array (numbers[i][j]). The first parameter is the row index and the second parameter is the column index. Arrays of higher dimensions can be constructed easily by adding more indexes.
Insert Operation
When inserting new values to the list, we just have to insert a value for the next index. This operation takes linear time complexity O(1). (e.g. Conducting the following insert operations on an empty array - Insert(34), Insert(12), Insert(120), Insert(-5).)
However, when we insert a value to a given index, let's say we insert 23 to the index 1 in the array, we have to move the values along the array in order to be able to insert a new one. This takes linear time complexity, O(n).
Remove Operation
When removing the last element from an array, we just have to delete the last. This operation takes linear time complexity O(1). (e.g. Removing -5 from the array.)
However, just like insertion at a given index, when we remove a value at a given index from an array, let's say we insert 23, we have to move the values along the array. This takes linear time complexity, O(n).
Summary
Advantages
Disadvantages
この記事が気に入ったらサポートをしてみませんか?