Home > Web Front-end > JS Tutorial > body text

Explanation of indexed arrays, associative arrays, static arrays, and dynamic arrays in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:31:57
Original
1218 people have browsed it

Array classification:

1. The subscript of the array is divided into index array and associative array

Copy code The code is as follows:

/* Index array, which is usually called an array */
var ary1 = [1,3,5,8];
//Get array elements by index, starting from 0 (of course some languages ​​start from 1)
//The index is actually an ordinal number, an integer number
alert(ary1[0]);
alert(ary1[1]);
alert(ary1[2]);
alert(ary1[3]);

/* Associative array refers to an array accessed with a non-ordinal type as a subscript. It is called a dictionary in Python */
var ary2 = {};
//When accessing, use non-ordinal numbers (numbers), here is a string
ary2["one"] = 1;
ary2["two"] = 2;
ary2["thr"] = 3;
ary2["fou"] = 4;

2. Data storage is divided into static arrays and dynamic arrays

Copy code The code is as follows:

// Static array in java
// After definition, the length of the array is fixed and cannot be changed. Access the array elements by index
Int[] ary1 = {1,3,6,9};

//Dynamic array in java
// The implementation of ArrayList in java is based on Array. Here, dynamic arrays are generalized, no matter what method is used to implement them.
List ary2 = new ArrayList();
ary2.add(1);//Elements can be added dynamically, and the length of the array also changes
ary2.add(3);
ary2.add(6);

Copy code The code is as follows:

/* js arrays are dynamic arrays */
var ary = [];//Define an array with unspecified length
ary[0] = 1; //Elements can be added dynamically
ary.push(3);
ary.push(5);

alert(ary.join(","));//Output 1,3,5

The array of js belongs to both index array and dynamic array, because it is essentially a js object, which embodies the dynamic language characteristics of js. However, the index array of js does not "continuously allocate" memory, so the indexing method does not bring high efficiency. Arrays in Java allocate memory continuously.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template