Index Array, associated array, static array, and dynamic array in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces index arrays, associated arrays, static arrays, and dynamic arrays in JavaScript, this article describes how to classify an array into an index array, an associated array, a static array and a dynamic array based on the subscript of the array, and provides an example. For more information, see array classification:

1. The subscript of an array is divided into an index array and an associated array.

The 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.

The 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 Ary2 = new ArrayList ();
Ary2.add (1); // You can dynamically add elements. The length of the array also changes.
Ary2.add (3 );
Ary2.add (6 );

The 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.