JavaScript indexed array associative array and static array dynamic array

  • 2020-03-30 04:14:02
  • OfStack

Array classification:

1, from the index of the array into the index array, associative array

var ary1 = [1,3,5,8];

//An index is really just an ordinal number, an integer number

var ary2 = {};
//When accessed, it is accessed as a non-ordinal number (number), in this case a string
ary2["one"] = 1;
ary2["two"] = 2;
ary2["thr"]  = 3;
ary2["fou"] = 4;

2. The storage of data is divided into static array and dynamic array

//Static array
in Java //After the definition of the length of the array fixed can not be changed, according to the index of the array element
Int[] ary1 = {1,3,6,9};
//Dynamic array in Java
//The ArrayList implementation in Java is based on an Array, and dynamic arrays are generalized, no matter how they are implemented. < br / > List<Integer> ary2 = new ArrayList<Integer>();
ary2.add(1);//Elements can be added dynamically, and the length of the array varies with

var ary = [];//Defines an array with an unspecified length
ary[0] = 1;//
can be added dynamically ary.push(3);
alert(ary.join(","));//Output 1,3,5 < br / >

Js array belongs to both indexed array and dynamic array, because it is essentially a js object, which embodies the dynamic language of js. However, the indexed array of js is not "continuously allocated" memory, so indexing is not very efficient. Arrays in Java allocate memory sequentially.

Related articles: