- Home /
How to declare and initialize multidimensional arrays JS?
I'm trying to instantiate a grid using cubes with this shape
I'm trying to use a multidimensional array for this but I have problems initializing the array with set values.
public var multiDimensionalArray: int[,] = [
[0,1,1,0],
[1,0,0,1],
[1,0,0,1],
[0,1,1,0]
];
Above is the first method I've tried but it throws the following error
Assets/TetrisPiece.js(7,45): BCE0022: Cannot convert 'int[][]' to 'int[,]'.
I've also tried storing an array in a array
var myArray: int[4];
myArray[0] = new int[0,1,1,0];
myArray[1] = new int[1,0,0,1];
myArray[2] = new int[1,0,0,1];
myArray[3] = new int[0,1,1,0];
But this shows the following error:
Assets/TetrisPiece.js(1,17): UCE0001: ';' expected. Insert a semicolon at the end.
I suspect a syntax error but I haven't been able to find what. Thanks in advance.
Answer by Eric5h5 · Mar 27, 2011 at 05:28 PM
Declaring an array the way you did initially results in a jagged array. If you leave off the type, type inference will type it as a jagged array automatically:
public var multiDimensionalArray = [
[0,1,1,0],
[1,0,0,1],
[1,0,0,1],
[0,1,1,0]
];
(The type for this is int[][], not Array.) Then you can do
multiDimensionalArray[0][1]
instead of
multiDimensionalArray[0,1]
Jagged arrays aren't quite the same thing as multidimensional arrays, but it would work well enough. If there's an easy syntax to initialize multidimensional arrays in JS by filling them with initial values, I don't know what it is. If you really need a "proper" multidimensional array, you might have to do it the hard way:
public var multiDimensionalArray : int[,];
multiDimensionalArray[0,0] = 0;
multiDimensionalArray[0,1] = 1;
...
Thanks. This at least got me as far as that I am able to replicate my image with cubes. So type inferencing will make it a jagged array. But if I set the type as int[][] I get an error. So I can't set the type myself?
For jagged arrays you can't explicitly set the type yourself in JS, which is an oversight, but it's irrelevant whether the type is derived from type inferencing or explicitly stating it--the compiled code is exactly the same either way. Note that type inferencing has nothing to do with dynamic typing, which is a different subject entirely, if that's what you're concerned about.
a somewhat similar problem .. ?
http://answers.unity3d.com/questions/286682/2d-array-persists-during-multiple-game-sessions.html
And what about to push other array to index like
multiDimensionalArray[4] = [1,0,1,0];
this code get the mistake "Array index is out of range."
Answer by dentedpixel · Nov 20, 2012 at 05:12 PM
public var multiDimensionalArray: int[,] = new int[4,4];
multiDimensionalArray[0,0] = 1;
multiDimensionalArray[0,1] = 1;
multiDimensionalArray[0,2] = 0;
multiDimensionalArray[0,3] = 1;
multiDimensionalArray[1,0] = 0;
....
multiDimensionalArray[3,3] = 0;
You can also turn this into a for loop to set the values.
Thanks! in my case I could not use either jagged arrays, nor Array or List classes, and it kept telling me I needed a semicolon. On the other hand, if I just said boolean[,], not all of my GUI would show up and none of the script would work. I never thought of this. Thanks!
Answer by mani444 · Aug 17, 2014 at 11:07 AM
I am trying this for making grid with each index containing card object but its not working. Same error.
function Start () {
var i:int = 0;
var j:int = 0;
aCards = new Array(); //Just Ignore it
aGrid = new Array();
aCardsFlipped = new ArrayList(); //Just Ignore it
for(i=0; i<4; i++)
{
aGrid[i] = new Array();
for(j=0; j<4; j++)
{
aGrid[i][j] = new Card(); //Assigning object of class card to grid
}
}
}
Answer by Meltdown · Mar 27, 2011 at 04:05 PM
JavaScript doesn't actually have support for multi-dimensional arrays, but you can nest arrays. For most programming purposes, nested arrays are equivalent to multi-dimensional arrays. Therefore we are not going to worry about the differences between them here.
To create a nested array, you just define an array element as being an array.
var outerA = new Array();
outerA[0] = new Array();
outerA[1] = new Array();
outerA[2] = new Array();
When using array literals, the same effect can be achieved by nesting square brackets.
var aName = [[1,2,3],['a','b','c']];
// is the same as
var aName = new Array(); aName[0] = [1,2,3]; aName[1] = ['a','b','c'];
Thanks a lot for the fast reply. I read that 3.2 added the support for multidimensional arrays. I have been able to use it to instantiate a grid of cubes using a nested for loop. Just haven't been able to initialize it with set values.
I see your using the javascript Array class in this example. How could I use this with build in arrays and what's the syntax for this? I read those are much faster then javascript arrays.
JS does have support for multidimensional arrays. There's very little reason to ever use the Array class; built-in arrays or generic Lists are almost always a better idea.
$$anonymous$$y bad :-( Guess my JS knowledge is a bit out of date then.
That's why I asked. Regardless though I'm having trouble accessing the arrays in this example.
var outerA = [ [0,1,1,0], [1,0,0,1], [1,0,0,1], [0,1,1,0] ];
How would I go about accessing this in a for loop?
Just to be nitpicky, JS always had support for multi-d arrays, but it wasn't possible to declare the type explicitly until Unity 3.2. This meant it was possible to use various workarounds involving type inference; see here: http://www.unifycommunity.com/wiki/index.php?title=Javascript$$anonymous$$ultiDimArrays