Array
Array is a set of numbered and fixed-length sequences of data items with the same unique type. This type can be any primitive type such as integer, string, or custom type.
Relative to declaring number0, number1,. .., number99 variables, using arrays numbers [0], numbers [1] …, numbers [99] is more convenient and easy to expand.
Array elements can be read (or modified) by index (position), index Starting from 0, the first element index is 0, the second index is 1, and so on.
The statement
that declares an array is as follows:
arr = [1, "Two", 3.0]
Access array elements
Array elements can be read by index (position). The format is the array name followed by brackets, and the brackets are the index value. For example:
print ('The third element is: ', ARR [2]);
- If the index exceeds the length of the exception will be thrown *
slices
can be obtained in the following manner array element portion
ARR = [. 1, "Two", "three", "IIII", 5.0001];
print (arr [1: 3]); // Get the element between the first and the third (open before and then open, get index 1, 2)
print (arr [: 2]); // Get the first two elements (get index 0, 1)
print (arr [3:]);
print (arr [-2:]); // Get the last two elements
print (arr [:-3]); // Get the last element before the third one
- When the index is out of range, it will not be thrown Exception, but return an empty array *
Iterate through
for (ind, ele in arr) (
print (` index = $ (ind), element = $ (ele) `)
)
Length and capacity
-Obtain the length of the array by the len method- Example of obtaining the capacity of the array by the cap method
arr = [1, 2, 3]; // Length print (len (arr)); print (arr.len); // Capacity print (cap (arr)); print (arr.cap);
Capacity does not represent the maximum length of the array, but increase the length within the capacity range will not re-apply for memory, no need to pay attention to novices
Array prototype method
Method name | Description | Example |
---|---|---|
forEach | Traverse the element | |
map | Traverse the element and reassemble the return value into an array | |
filter | Traverse the element And remove the content that returns zero and reorganize |
| filterMap | Traversing elements and reorganizing elements that return non-false | asyncMap | Asynchronous multi-threaded execution of tasks