以下是在 typescript 中创建布尔数组的不同方法:
let arr1: boolean[] = [];
let arr2: boolean[] = new Array();
let arr3: boolean[] = Array();
let arr4: Array<boolean> = [];
let arr5: Array<boolean> = new Array();
let arr6: Array<boolean> = Array();
let arr7 = [] as boolean[];
let arr8 = new Array() as Array<boolean>;
let arr9 = Array() as boolean[];
let arr10 = <boolean[]>[];
let arr11 = <Array<boolean>> new Array();
let arr12 = <boolean[]> Array();
let arr13 = new Array<boolean>();
let arr14 = Array<boolean>();
您可以使用索引访问它们:
console.log(arr[5]);
并且您使用 push 添加元素:
arr.push(true);
创建数组时,可以提供初始值:
let arr1: boolean[] = [true, false];
let arr2: boolean[] = new Array(true, false);