4. Array (top)

Source: Internet
Author: User

1. Understanding Arrays

/
Array
Store a large push of data.
Why use arrays: Store all the grades in the whole class.
"Note" There is the need to process bulk data of the same type, invent arrays, and store bulk data.
Data type:
= = Basic Data type: = =
+ + numeric, string, Boolean + +,
= = Special Data type: = =
(++null undefined nan++)
= = Composite data type: = =
+ + arrays, functions. ++
/

/*
Grammar:
Array: Array shorthand arr
Note: Although you can store any type of value, in general, bulk data of the same type is stored.
Declaring an array

1. Declaration by the New keyword
                括号里面,可以传,你要去存储的数据,值用逗号隔开。  

"Note" If there is only one data in the parentheses, and the data is a number, declare the length of the array I want to apply.
*/

var arr1 = new Array(10);            alert(arr1);            // var arr1 = new Array(10, true, "hello");            // alert(arr1); //10,true,hello
2. Declaration of the New keyword
        var arr2 = Array(10, true, "hello");        // alert(arr2); //10,true,hello
3. Declaring arrays

The constant form of the array [data 1, Data 2, data 3];

var arr3 = [10, true, "hello"];            // alert(arr3);                        /*                arr1 arr2 arr3 数组名字                元素: 每一个数据/每一位住户             */            /*                找房间去访问元素                官方:通过下标去访问元素。房间号是从0开始的。                格式:                    数组名[下标];                    【注】就是当做一个普通变量使用。            */            /*alert(arr3[1]);            arr3[1] = "world";            alert(arr3);*/
2. Operation of arrays

Length
The function is: the length of the output array.
Format: Array. length

// var arr = [10, 20, 30, 40, 50];            // alert(arr.length);            //arr[0] arr[1]

Traversal of an array (accessed individually)
"Note" Loops and arrays are a good couple.
"Note" Traversal access.

        /*for (var i = 0; i < arr.length; i++) {alert (arr[i]);            }*//* 1, declares a 10-length array of 2, each element, and stores a 0~9 random number.            Math.random ();        "Note" Iterates over the assignment.        *//*var arr = new Array (10);        alert (arr.length);            for (var i = 0; i < arr.length; i++) {//Generate random number var tmp = parseint (Math.random () * 10);        Arr[i] = tmp;        } alert (arr),/*/* 1, an array with a declaration length of 25 stores the number of 1~25 2, and then iterates through the array, summing the elements in the array.        */var arr = new Array (25);                Alert (arr.length)//assignment for (var i = 0; i < arr.length; i++) {Arr[i] = i + 1;        } alert (arr);        sum var sum = 0;        for (var i = 0; i < arr.length; i++) {sum + = Arr[i]; } alert (sum);        325/* "NOTE" for...in is faster than for-follow.            "Note" Fast traverse/Fast enumeration I is the subscript.            Format: for (var i in arr) {                } */var arr = [10, 20, 30, 40, 50];        for (Var j in arr) {alert (Arr[j] + "," + j);         }
For in

/
The For Loop focuses on loops that can be used to calculate
for...in Traversal (BUG) traversal, there is no action on the array.
/

/
Splice (start, length, data 1, data 2 ...)
Values: arrays, arrays, elements to intercept
/

            //增              var arr = [10, 20, 30, 40];            /*arr.splice(1, 0, 90, 100);            alert(arr);*/                        //改(先删除后增加)            /*arr.splice(1, 2, 90, 100);            alert(arr);*/            //删除            var newArr = arr.splice(1, 1);            alert(arr);            alert(newArr.length);                        var arr1 = [10, 20, 30];
3. Array method

Stack of wooden pots
Features: From one end, take from the same head.
Features: Advanced post-out.
The array is modeled by means of a stack structure.

Push ()
The "format" array. push ()
"In parentheses, data": The number of data is arbitrary, comma-separated
"function" adds an element to the end of the original array.
The length of the array after the element is added after the "Call function later value".

var arr = ["钢铁侠", "星爵", "猩红女巫"];                var result = arr.push("格鲁特", "蚁人", "金刚狼");                // alert(arr);                alert(result);

Pop ()
"Format" array. Pop ()
No data is passed in parentheses
function takes an element from the end of the array and can fetch only one element at a time.

/*var arr = ["钢铁侠", "星爵", "猩红女巫", "美国队长"];            var result = arr.pop();            alert(result);            alert(arr);*/

Queue: From a head in, out from the other, called the queue.
Features: Advanced First Out

Push
Shift ()
"Format" array. Shift ()
"function" takes an element from the head of an array and can fetch only one at a time.

var arr = ["钢铁侠", "星爵", "猩红女巫", "美国队长"];                var result = arr.shift();                alert(arr);                alert(result);

Unshift ()
The "format" array. Unshift ()
"Function" from the head of the array to interpolate data, you can pass data, the number of data arbitrary, separated by commas.
The length of the array after the element is added after the "Call function later value".

var arr = ["钢铁侠", "星爵", "猩红女巫", "美国队长"];            var result = arr.unshift("绿巨人", "雷神");            alert(arr);            alert(result);                                    /*var num1 = 10;            var num2 = num1;            num2 = 20;            alert(num1);            alert(num2);*/

Data type:
Basic data types
Composite data type/reference data type
Other languages are called pointers.
Note: The composite data type is not directly assigned, but is essentially an assignment of the address.

/*          var arr1 = [10, 20, 30];            var arr2 = arr1;            arr2.push(50);            alert(arr1);            alert(arr2);*/

==concat () = =
The "format" array. Concat ()
"Parenthesis Data" + + data is an array, each element of the array is spliced, and a new array of new successes is made. ++
= = "function" clones a new array = =

/*          var arr1 = [10, 20, 30];            var arr2 = arr1.concat();            arr2.push(50);            alert(arr1);            alert(arr2);*/            /*var arr1 = ["美国队长", "黑寡妇"];            var arr2 = ["钢铁侠", "绿巨人"];            var arr = arr1.concat(arr2);            alert(arr);            alert(arr1);            alert(arr2);*/

==slice==
The "format" array. Slice (start, end); [Start, end]
+ + "Function" adds the array subscript = = from start to end, but does not include the end position of the element ==,== is extracted, a new array is generated. ==++

/*          var arr = [10, 20, 30, 40, 50];            var newArr = arr.slice(1, 3);            alert(newArr);            alert(arr);*/

==splice () = =
The "format" array. Splice (start, length, element 1, Element 2 ...);
+ + First incoming data start: starting subscript
The second passed parameter length:
The element to pass in, starting with the third passed argument. ++
= = "Function" can be based on different communication parameters to achieve increase, delete, change = =
The element that "value" intercepts.

 //var arr = [];            Delete/*var arr = [10, 20, 30, 40, 50];            var result = Arr.splice (1, 2);            Alert (arr);            alert (result); *//increase/*var arr = [10, 20, 30, 40, 50];            var result = Arr.splice (2, 0, 90, 100);            Alert (arr);            alert (result); *//change/*var arr = [10, 20, 30, 40, 50];            Arr.splice (2, 1, 100); Alert (arr); */ 
Defines an array of 30 integer elements, giving an even number starting from 2 in order, and then averaging each of the five numbers in order, placing them in another array and outputting them.            Test programming.            /*var arr = new Array (30);            1. Fill the array with elements for (var i = 0; i < arr.length; i++) {Arr[i] = (i + 1) * 2; } alert (arr); */var arr = [];                Declares an empty array for (var i = 0; i < i++) {Arr.push ((i + 1) * 2);            Arr[i] = (i + 1) * 2;            } alert (arr);             Method of deposit Mean one/*var Averagesarr = [];                for (var i = 0; i < 6; i++) {//Take five-bit elements out of var Tmparr = Arr.splice (0, 5);                var sum = 0;                Calculates the average for (var j = 0; J < Tmparr.length; J + +) {sum = sum + tmparr[j];            } averagesarr.push (SUM/5); } alert (Averagesarr); */Method two var sum = 0;            Sum each of the five elements for once and var averagesarr = [];           for (var i in arr) {sum + = Arr[i];     i++;                    if (i% 5 = = 0) {//Every five number will be executed once here Averagesarr.push (SUM/5);                sum = 0; }} alert (Averagesarr); 6,16,26,36,46,56

Slice
function: extracts the corresponding range of elements to form a new array. Has no effect on the original array.
Format: Array. Slice (start, end) [Start,end]

Splice
function: intercept and intercept elements directly from the original array.
Format: Array. Splice (start, length);

Reverse () change the original array;

4. Memory

Basic Data type Memory analysis

Composite data type Memory analysis

Memory analysis

                    外设                    CPU                     GPU(显卡)                     主板                    内存                    硬盘                    硬盘 -> 内存                    内存运行程序环境                    程序被CPU运行                    程序读到内存里面  CPU必须先找到程序才能运行。                    32位                    64位                    可最大寻址基数                    每一个1或者0 每一个开关到要进行编号。                var num = 10;
Computer Fundamentals

?? The fundamentals of the computer are: stored programs and program control. A sequence of instructions (called a program) and raw data that directs the operation of the computer in advance is delivered to the computer's memory via the input device. Each instruction explicitly specifies which address the computer takes from, what to do, and what address to send to.

?? + + von Neumann structure + + is also known as the Princeton structure, a memory structure that merges program instruction memory and data memory together. The program instruction store address and data storage address point to different physical locations of the same memory, so the program instruction and data are the same width, such as Intel Corporation's 8086 CPU program instructions and data are 16 bits wide.

?? When the computer is running, the first instruction is removed from the memory, and the decoding of the controller, according to the requirement of the instruction, takes the data from the memory to perform the processing of the specified arithmetic and logic operation, and then sends the result to the memory by the address. Next, take out the second instruction and complete the specified operation under the command of the controller. Proceed accordingly. Until a stop command is encountered.

?? The program and data storage, according to the Order of the program, step-by-step take out the instructions, automatic completion of the instructions prescribed by the computer is the most basic principle of operation. This principle was originally proposed by the Hungarian-American mathematician von Neumann in 1945, so it is called von Neumann principle.

4. Array (top)

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.