x


How to declare and initialize multidimensional arrays JS?

I'm trying to instantiate a grid using cubes with this shape

alt text

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.

more ▼

asked Mar 27 '11 at 03:57 PM

Joeri gravatar image

Joeri
43 1 2 8

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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;
...
more ▼

answered Mar 27 '11 at 05:28 PM

Eric5h5 gravatar image

Eric5h5
80k 41 131 518

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?

Mar 27 '11 at 05:45 PM Joeri

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.

Mar 27 '11 at 06:03 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
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.

more ▼

answered Nov 20 '12 at 05:12 PM

dentedpixel gravatar image

dentedpixel
141 2

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!

Jan 27 at 10:59 PM Zarenityx
(comments are locked)
10|3000 characters needed characters left

Nikhilesh Kumar Patidar(Asp.net code):-

<head runat="server">>
    <script type="text/javascript">
        function displayResult()
        {
            var myarray=new Array(4);
            var myarray1=new Array(4);
            myarray1[0]="Apple";
            myarray1[1]="Pear";
            myarray1[2]="Banana";
            myarray1[3]="Orange";
            var L=100;
            for(i =0; i<myarray1.length;i++)
            {
                myarray[i] = new Array(2);
                myarray[i][0]=myarray1[i];
                myarray[i][1]=L+100;
                L=L+100;
            }
            var x=document.getElementById("mySelect");
            var a=document.getElementById("Select1");
            var option=document.createElement("option");
            var b='';
            var k;
            option.text="450";
            var d=0;
            var g=0;
            d=x.selectedIndex;
            b=x.options[d].value;
            var n=0;
            var m='';
            var c='';
            for (i=0;i<=a.length+1;i++)
            {
                a.remove(a.options[i]);
            }
            for(i=0; i<myarray.length;i++)
            {   
                m=myarray[i][0];
                c=myarray[i][1];
                if(b==m)
                {
                    a.options[a.options.length]= new Option(c, n);
                    n++;
                }
            }



        }
    </script>
</head>


<form id="form1" runat="server">
    <select id="mySelect" onchange="displayResult();">
      <option>Apple</option>
      <option>Pear</option> 
      <option>Banana</option>
      <option>Orange</option>
    </select>

    <select id="Select1">
    </select>
</form>

<%--<button type="button" onclick="displayResult()">Insert option</button>--%>
more ▼

answered Feb 11 at 05:21 PM

nikhilesh gravatar image

nikhilesh
1

(comments are locked)
10|3000 characters needed characters left

Nikhilesh Patidar:-

function displayResult() { var myarray=new Array(4); var myarray1=new Array(4); myarray1[0]="Apple"; myarray1[1]="Pear"; myarray1[2]="Banana"; myarray1[3]="Orange"; var L=100; for(i =0; i<myarray1.length;i++) { myarray[i] = new Array(2); myarray[i][0]=myarray1[i]; myarray[i][1]=L+100; L=L+100; } var x=document.getElementById("mySelect"); var a=document.getElementById("Select1"); var option=document.createElement("option"); var b=''; var k; option.text="450"; var d=0; var g=0; d=x.selectedIndex; b=x.options[d].value; var n=0; var m=''; var c=''; for (i=0;i<=a.length+1;i++) { a.remove(a.options[i]); }

            for(i=0; i<myarray.length;i++)
            {   
                m=myarray[i][0];
                c=myarray[i][1];
                if(b==m)
                {
                    a.options[a.options.length]= new Option(c, n);
                    n++;
                }
            }



        }
more ▼

answered Feb 11 at 06:00 PM

nikhilesh gravatar image

nikhilesh
1

(comments are locked)
10|3000 characters needed characters left

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'];
more ▼

answered Mar 27 '11 at 04:05 PM

Meltdown gravatar image

Meltdown
5.6k 18 25 49

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.

Mar 27 '11 at 04:31 PM Joeri

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.

Mar 27 '11 at 05:15 PM Eric5h5

My bad :-( Guess my JS knowledge is a bit out of date then.

Mar 27 '11 at 05:24 PM Meltdown

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?

Mar 27 '11 at 05:29 PM Joeri

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=JavascriptMultiDimArrays

Mar 27 '11 at 05:44 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3442
x265
x60
x29
x28

asked: Mar 27 '11 at 03:57 PM

Seen: 20931 times

Last Updated: Feb 11 at 06:00 PM