[英]How do I declare a Multidimensional Array of a given size in Typescript
I have the following... 我有以下...
export class Puzzle {
pieces : Piece[];
orderedPieces : Piece[][];
constructor(width: number, height: number){
this.height = height, this.width = width;
let total : number = width * height;
this.pieces = new Array<Piece>(total);
this.orderedPieces = new Piece[height][width]; // Doesn't work what do I put here?
}
...
}
How would I declare something like this in Typescript? 我如何在Typescript中声明这样的内容?
Error is... 错误是...
Cannot read property '2' of undefined
无法读取未定义的属性“ 2”
There's no way (that I'm aware of) to instantiate a multidimensional array in javascript in one line. (我知道)无法在一行中实例化javascript中的多维数组。 You'll have to actually create all of the arrays yourself, something like:
您必须自己实际创建所有数组,例如:
this.orderedPieces = new Array(this.height);
for (let i = 0; i < this.height; i++) {
this.orderedPieces[i] = new Array(this.width);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.