Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  65] [ 4]  / answers: 1 / hits: 90661  / 12 Years ago, thu, march 15, 2012, 12:00:00

I would like to know how to declare 2 dimensional arrays in java script of fixed lengths something like,


var array = new Array[this.rows, this.columns];

after that I would like to get the length of each dimension like,


array[0].length; // I am assuming this would give me the first dimension like in C# array.GetLength(0);
array[1].length; // I am assuming this would give me the second dimension like in C# array.GetLength(1);

More From » arrays

 Answers
9

A quick example of what @SLaks is referring to with jagged arrays. You basically put an array in an array. The below example shows one way to make an array thats 100x100.



var arr = [];
for(var x = 0; x < 100; x++){
arr[x] = [];
for(var y = 0; y < 100; y++){
arr[x][y] = x*y;
}
}

console.log(arr[10][11]);


Live Demo



This method is very flexible for instance arr[4] could have an array indexed to 10, and arr[5] could have an array with 1 value, or be even be a completely different type such as a string or number.


[#86824] Wednesday, March 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mary

Total Points: 432
Total Questions: 98
Total Answers: 98

Location: Luxembourg
Member since Tue, Jan 25, 2022
2 Years ago
;