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
Answer
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;
}
No comments:
Post a Comment