Index Array, associated array and static array in JavaScript, dynamic array explanation, and javascript Index
Array category:
1. The subscript of an array is divided into an index array and an associated array.
Copy codeThe Code is as follows:
/* Index array, which is usually an array */
Var ary1 = [1, 3, 5, 8];
// Retrieve array elements by index, starting from 0 (of course, some languages start from 1)
// The index is actually a ordinal number, an integer.
Alert (ary1 [0]);
Alert (ary1 [1]);
Alert (ary1 [2]);
Alert (ary1 [3]);
/* Associated array, which is an array that is accessed by subscripts of the non-ordinal number type. In python, it is called a dictionary */
Var ary2 = {};
// The value is a non-sequential number (number). Here it is a string.
Ary2 ["one"] = 1;
Ary2 ["two"] = 2;
Ary2 ["thr"] = 3;
Ary2 ["fou"] = 4;
2. Data Storage is divided into static and dynamic arrays.
Copy codeThe Code is as follows:
// Static array in java
// After definition, the length of the array is fixed and cannot be changed. Retrieve array elements by index
Int [] ary1 = {1, 3, 6, 9 };
// Dynamic array in java
// The ArrayList implementation in java is based on Array. Here, dynamic arrays are implemented in a broad sense, regardless of the method used.
List <Integer> ary2 = new ArrayList <Integer> ();
Ary2.add (1); // You can dynamically add elements. The length of the array also changes.
Ary2.add (3 );
Ary2.add (6 );
Copy codeThe Code is as follows:
/* Js arrays are dynamic arrays */
Var ary = []; // defines an array with no Length Specified
Ary [0] = 1; // You can dynamically add elements.
Ary. push (3 );
Ary. push (5 );
Alert (ary. join (","); // output 1, 3, 5
The js array belongs to both an index array and a dynamic array, because it is essentially a js object, reflecting the Dynamic Language Characteristics of js. However, the index array of js is not "continuously allocated" memory, so the indexing method does not bring high efficiency. The arrays in java are continuously allocated memory.