Create a two-dimensional array and a simple example

I. Definitions

In another one-dimensional array which define a plurality of one-dimensional array, the array is two-dimensional. (This definition may be a bit one-sided, but the principle of a two-dimensional array of roughly like this, in many languages, for a two-dimensional array has a three-dimensional array, or a multi-array, all this in mind, such as c language, c ++, etc. but in a different language, it may be slightly different definition of the way).

eg : 

  was arr = [

    [1,2,3],

    [4,5,6],

    [7,8,9],

    [10,11,12]

  ]

Second, the practice of two-dimensional array

  1. Line-sequentially by the circulation of a 5 × 5 two-dimensional array forming a number of samples of natural programmed 1-25.

    Analysis: 1. to create a one-dimensional array arr  2. use cycle, to a array, push5 array, thus creating a two-dimensional array        3. After, think of ways to natural numbers from 1 to 25, into an array        4. returns an array of function Fn () { var ARR = []; var = COUNT. 1; for (var I = 0; I <. 5; I ++) { arr.push ([]); for (var J = 0; J <. 5; J ++) { // ARR [I] [J] COUNT = ++; ARR [I] .push (COUNT ++); } } return ARR; } the console.log (Fn ());















  2. 30 define an array of integers containing elements, respectively, in order to impart starting from an even 2; and the order number is obtained a mean value for every five, in another array. Test program.

    Analysis: 1. define three arrays, used to store raw data arr1 [], arr2 [arr1 used to store the number of each of five divided], meanValue [] for storing the average value. 
    2. The original array data, each of five divided, placed in arr2.
    3. arr2 for each group of data averaging, into the meanvalue
    4. Return meanvalue.
    Function Fn () {
    var ARR = [];
    var arr2 = [];
    var meanvalue = [];
    for (var 2 = I; I <= 60; I = + 2) { arr.push (I); } for (var I = 0; I <. 6; I ++) { arr2.push (arr.splice (0,5)) ; } for (var I = 0; I <arr2.length; I ++) { var SUM = 0; for (var J = 0; J <arr2 is [I] .length; J ++) { SUM + = arr2 is [I] [ J]; } meanValue.push (SUM /. 5); } return meanvalue; } the console.log (Fn ());















  3. Line-sequentially by the circulation of a 5 × 5 two-dimensional array forming a natural number from 1 to 25, and then outputs the lower left half of the array and the triangle. Test program.

    Analysis: 1 give a two-dimensional array of 5 * 5 Assignment 
    2. demand in the second half and triangular elements.
    Fn function () {
    var ARR = [];
    var = COUNT. 1;
    var SUM = 0;
    for (var I = 0; I <. 5; I ++) { arr.push ([]); for (var J = 0; J <. 5; J ++) { ARR [I] .push (COUNT ++); } } for (var I = 0; I <. 5; I ++) { for (var J = 0; J <= I; J ++) { SUM + ARR = [I] [J]; } } return SUM; } the console.log (Fn ());












  4. Writing functions norepeat (arr) to remove elements of the array will be repeated

    Analysis: 1 using the selection sort of thought, each array of data to compare, if two identical elements, the elements behind the delete 
    2. After the element removal, let the elements behind to move forward after, the elements needed to judge the current position again.
    3. Return the original array.
    ARR = var [1,2,5,4,6,3,2,1,4,5,2,3,6,5,4,1,2,3,2];
    function norepeat (ARR) {
    for (var I = 0; I <-arr.length. 1; I ++) { for (var = I + J. 1; J <arr.length; J ++) { IF (ARR [I] == ARR [J]) { ARR .splice (J,. 1); J,; } } } return ARR; } the console.log (norepeat (ARR));









  5. Encapsulation function "there is a small to a large array of sorted now to enter a number, required by the laws of the original insert it into the array"

    var arr = [1, 2, 3, 4, 5, 7, 8, 9];
    function fn(arr, k) {
    var flag = true;
    for (var i = 0; i < arr.length; i++) {
    if (arr[i] >= k) {
    arr.splice(i, 0, k);
    flag = false;
    break;
    }
    }
    if (flag == true) {
    arr.push(k);
    }
    return arr;
    }
    console.log(fn(arr, 410));

 

Guess you like

Origin www.cnblogs.com/lxz123/p/11494223.html