Typescript - 多维数组初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30144580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:39:30  来源:igfitidea点击:

Typescript - multidimensional array initialization

multidimensional-arraytypescript

提问by Fka

I'm playing with Typescript and I wonder, how to properly instantiate and declare multidimensional array. Here's my code:

我正在使用 Typescript,我想知道如何正确实例化和声明多维数组。这是我的代码:

class Something {
    private things: Thing[][];

    constructor() {
        things = [][]; ??? how instantiate object ???

        for(var i: number = 0; i < 10; i++) {
            this.things[i] = new Thing[];   ??? how instantiate 1st level ???
            for(var j: number = 0; j< 10; j++) {
                this.things[i][j] = new Thing();   ??? how instantiate 2nd lvl item ???
            }
        }
    }
}

Can you give me any hint about selected places?

你能给我一些关于选定地点的提示吗?

回答by Puppy

You only need []to instantiate an array - this is true regardless of its type. The fact that the array is of an array type is immaterial.

您只需要[]实例化一个数组 - 无论其类型如何,都是如此。数组是数组类型这一事实并不重要。

The same thing applies at the first level in your loop. It is merely an array and []is a new empty array - job done.

同样的事情适用于循环的第一级。它只是一个数组,[]是一个新的空数组 - 工作完成。

As for the second level, if Thingis a class then new Thing()will be just fine. Otherwise, depending on the type, you may need a factory function or other expression to create one.

至于第二层,如果Thing是一个班级new Thing()就可以了。否则,根据类型,您可能需要一个工厂函数或其他表达式来创建一个。

class Something {
    private things: Thing[][];

    constructor() {
        this.things = [];

        for(var i: number = 0; i < 10; i++) {
            this.things[i] = [];
            for(var j: number = 0; j< 10; j++) {
                this.things[i][j] = new Thing();
            }
        }
    }
}

回答by RMuesi

If you want to do it typed:

如果你想这样做,请输入:

class Something {

  areas: Area[][];

  constructor() {
    this.areas = new Array<Array<Area>>();
    for (let y = 0; y <= 100; y++) {
      let row:Area[]  = new Array<Area>();      
      for (let x = 0; x <=100; x++){
        row.push(new Area(x, y));
      }
      this.areas.push(row);
    }
  }
}

回答by techguy2000

Here is an example of initializing a boolean[][]:

下面是一个初始化 boolean[][] 的例子:

const n = 8; // or some dynamic value
const palindrome: boolean[][] = new Array(n).fill(false).map(() => new Array(n).fill(false));

回答by Pipo

Beware of the use of push method, if you don't use indexes, it won't work!

小心使用push方法,不使用索引是不行的!

var main2dArray: Things[][] = []

main2dArray.push(someTmp1dArray)
main2dArray.push(someOtherTmp1dArray)

gives only a 1 line array!

只给出一个 1 线阵列

use

main2dArray[0] = someTmp1dArray
main2dArray[1] = someOtherTmp1dArray

to get your 2d array working!!!

让你的二维数组工作!!!

Other beware! foreach doesn't seem to workwith 2d arrays!

其他小心!foreach 似乎不适用于二维数组!