0

How to declare , initialize and use 2 dimensional arrays in javascript, If I write

var arr=new array(2,2)

is it correct to declare a array with 2 rows and two columns

and is it fine if i declare an array variable globally and allocate space in some function. or is the following is correct?

var arr;
//Some operations
function(){
arr[0][1]=8;
};

I dont know how can you mark it as duplicate please read the description of the question not the title i want to declare the array without initializing it are you getting my point if not please ........ you are pointing it to the static declaration of an array and that i know and i learnt from the basics of programming 10 years ago so if you dont know know English kindly go to some language school and come back

2 Answers2

0

There is no "matrix" structure natively in the language. But you can create them without major problem as far as you "book" the required memory.

Let's say you would like to create a 3x3 matrix, first you have to create an Array which will store references to each row/column (depending of your point of view, of course).

function createMatrix(N, M) {
    var matrix = new Array(N); // Array with initial size of N, not fixed!

    for (var i = 0; i < N; ++i) {
        matrix[i] = new Array(M);
    }

    return matrix;
}
José Cabo
  • 6,149
  • 3
  • 28
  • 39
  • How about a multidimensional array by taking the no of rows and columns as from html page lets a text field –  Aug 03 '15 at 15:55
  • You should use the same pattern for multidimensional arrays. For each dimension, you require to create an array for it. Regarding the text input, that is another question. – José Cabo Aug 03 '15 at 15:56
0

You generally don't use new Array like that. A new Array constructor may take only one parameter: the length. Or take multiple parameters wich will be the values of the array.

Passing just the length will produce an array with as many undefined values as specified in the parameter. Not really interesting. It can be if you create js types array like that : var myArray = new Uint8Array(10). But not for what you try to do.

Passing the values will produce the same thing as creating the array as follow :

var myArray = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ]
];

And then , myArray[0][1] will be 2.

Or the same :

var myArray = [];
myArray.push( [ 1, 2, 3 ] );
myArray.push( [ 4, 5, 6 ] );

It will produce the same result.

Remember that this is not C++ or R. The elements in your array don't have a particular type. They can be anything. A function, a number, an object... And in this case, another array. These are not real multidimentionnals arrays or matrices. Just arrays where some of the elements are arrays.

You can nest as much as you want.

fabien
  • 2,228
  • 2
  • 19
  • 44
  • This is static, how can you declare an array by using a value from the html page. –  Aug 03 '15 at 15:54
  • "A new Array constructor take only one parameter" is wrong, you can pass any number of arguments to [Array constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax) – Teemu Aug 03 '15 at 15:56
  • Yes, you are right. But this is not usually how you create an Array in JS. I edit my answer. @Srikanth Suryadevara : you will have to be more specific. You problem is probably more a DOM manipulation problem than a pure js problem. – fabien Aug 03 '15 at 17:16
  • Hi lets say how to declare an 2 dimensional array with 50 rows and 50 columns. How to insert a value to the specific location lets say 45th row 23 element. –  Aug 03 '15 at 17:40
  • `myArray[45][23] = value;` – fabien Aug 04 '15 at 18:26