2017-11-04 12:22:15

Hi guys!
Please give examples of the initialization of multidimensional arrays.
int[][] MMArray;
How do I specify the size of an array?
How to work with dynamic arrays with insert_at, insert_last, etc?
Thanks in advance!

2017-11-04 13:11:51

With multidimentional arrays, you can't really initialize their sizes with constructors. However, remember that a 2 dimensional array is just an array of arrays. So mmArray.resize(10) resizes the outer array / the first index. You would need to resize each inner array individually, for example, with a for loop.

for (uint i=0; i<mmArray.length(); i++) mmArray[i].resize(10); 

Insertion and removal work using the same concept. If you want to insert something at [1][2], you would say mmArray[1].insert_at(2, value);

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2017-11-04 13:59:06

mmArray[i].resize(10);

this one-dimensional array, I'm interested in for example a two-dimensional array

2017-11-04 14:48:42

Consider this:
int[][] map = get_map();
int[] row = map[0];
map[1] = row;
if (map[0][0] != map[1][0]) {
    alert("fatal error", "BGT is broken!");
}
Since a 2 dimensional array is a 1 dimensional array whose type is int[], you can pass around rows from the 2 dimensional array as though they are 1 dimensional arrays. this is because they are.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.