Digital Cube and array concepts

Source: Internet
Author: User
Tags array length

Digital Cube (Classic data structure instance ):
1. The gameplay of this game is described in this FLASH, and the blog will not repeat it:
Download address:
Http://www.flashempire.com/myfe/upload/flash/142/1410014_1195878655.swf
2. The basic principle of programming is the gameplay principle:
1) the middle of the first line is 1;

2) 1 to the right; 1 to the top;

3) go to the bottom of the window;

4) go out of bounds on the right and go to the left;
5)
Fill in the number if no quantity is occupied;

6) sit down and sit down.

3. The following is a matrix computing program:
Var n: Number =
5;
// The number of columns in The Matrix is n * n;

Var matrix: Array = new
Array ();
// Because the two-dimensional array cannot be directly called in AS, we apply for a one-dimensional array first;


For (var I = 0;
I <n; I ++ ){

Matrix [I] = new Array ();

For (var j = 0; j <n; j ++ ){

Matrix [I] [j] = I * n + j;

}

}
// Functions of the code above: converts a one-dimensional array into a two-dimensional array (a functional code );

For (var I =
0; I <n; I ++ ){

For (var j = 0; j <n; j ++ ){

Matrix [I] [j] = 0;

}

}
// Initialize a two-dimensional array so that each element in the matrix is 0;


Var movei: Number = 0;

Var movej: Number = int (n/2 );

Matrix [movei] [movej] =
1;
// The first step of the general principle is completed here [1 in the middle of the first line];

Var counter: Number =
2;
// Counter, ready to be placed in the next number 2

For (I = 0; I <n;
I ++ ){

For (j = 0; j <n; j ++ ){

Tempi = movei --;

Tempj =
Movej ++;
// General principle step 1 [1 to the right; 1 to the top]


If (movei <0 ){

Movei =
N-1;
// General principle step 1 [go out of bounds above, go down]


  }

If (movej> n-1 ){

Movej =
0;
// General principle step 1 [exit on the right, go to the left]


  }

If (matrix [movei] [movej] = 0)
{

Matrix [movei] [movej]
=
Counter ++;
// General principle step 1 [fill in the number if no number is occupied]


} Else {

Movei =
Tempi + 1;

Movej =
Tempj;

Matrix [movei] [movej]
=
Counter ++;
// General principle step 1 [count and sit down]


  }

}

}
// ------------ Check the final output matrix --------------


For (I = 0; I <n; I ++ ){

Trace (matrix [I]);

}




Flash charging 1: in AS, the subscript of the array is the same AS that in C language, and starts from 0.
Here is an example:
Var
Myarray: Array = ["CAT", "DOG", "PIG", "BIRD", "MONKEY"];


Trace (myarray [3]);
// BIRD is displayed after the test



Flash Charging 2: There are two concepts in the data structure: Stack and queue.
In AS, these two concepts are also completed using arrays.
Stack (back-to-first-out ):
1. Output stack:
Pop (): After extracting the last element, delete it.
Var
Myarray: Array = ["CAT", "DOG", "PIG", "BIRD", "MONKEY"];
Trace (myarray. pop ())
// Display the MONKEY, and then delete the last element "MONKEY"
2. Stack entry:
Push (): insert an element after the last element
Var
Myarray: Array = ["CAT", "DOG", "PIG", "BIRD", "MONKEY"];
Myarray. push ("FISH ")
Trace (myarray)
// Display CAT, DOG, PIG, BIRD, MONKEY, and FISH. Add a "FISH" next to the last element"

Queue (first-in-first-out ):
Shift (): After extracting the first element, delete it.
Var
Myarray: Array = ["CAT", "DOG", "PIG", "BIRD", "MONKEY"];
Trace (myarray. shift ())
// Display CAT and delete the first element "CAT"

Flash Charging 3: common array operations.
Var
Myarray: Array = ["CAT", "DOG", "PIG", "BIRD", "MONKEY"];
Trace (myarray. length)
// Display 5 to get the array length;
Trace (myarray. join ("+ "));
// Display CAT + DOG + PIG + BIRD + MONKEY, connect the array with "+", of course, it can also be set as other symbols
Trace (myarray. reverse ());
// Display MONKEY, BIRD, PIG, DOG, CAT, array inverted sorting

This is the end of the tutorial.
QQ: 147461195 (FL Foundation master)

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.