Monday, April 29, 2019

javascript - How to declare initialize and use 2 dimensional arrays in Java Script Using a text from html page





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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...