简体   繁体   English

如何用值初始化类型化数组?

[英]How to initialize a typed array with values?

Assemblyscript initializes typed arrays with their length, like this: Assemblyscript 用它们的长度初始化类型化的 arrays,如下所示:

var arr = new Uint8Array(2);

Suppose I want to seed this array with some values, like [12, -47, 91], how would I do it?假设我想用一些值作为这个数组的种子,比如 [12, -47, 91],我该怎么做?

I found the answer myself.我自己找到了答案。 One must use StaticArray like this:必须像这样使用StaticArray

var arr:StaticArray<i8> = [12, -47, 91];

You can use this approach if you need typed array instead StaticArray如果您需要类型化数组而不是StaticArray ,则可以使用此方法

const arr = new Uint8Array(3);
arr.set([12, -47, 91]);

Ignore Argument of type 'number[]' is not assignable to parameter of type 'ArrayBufferView'. Argument of type 'number[]' is not assignable to parameter of type 'ArrayBufferView'. warning for now.暂时警告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 AssemblyScript 通过引用操作 canvas 数据(类型化数组) - Manipulating canvas data (typed arrays) by reference using AssemblyScript 在 WebAssembly 和 JavaScript 之间传输字节数组(Uint8Array) - Transfer byte array (Uint8Array) between WebAssembly and JavaScript 错误 TS2322:类型 &#39;~lib/array/Array&lt;~lib/string/String&gt; | null&#39; 不可分配给类型 &#39;~lib/array/Array&lt;~lib/string/String&gt;&#39; - ERROR TS2322: Type '~lib/array/Array<~lib/string/String> | null' is not assignable to type '~lib/array/Array<~lib/string/String>' AssemblyScript 是否支持函数数组? - does AssemblyScript supports array of functions? 分配 65536 个元素的数组后,AssemblyScript / WebAssembly 分配失败 - AssemblyScript / WebAssembly fails allocation after 65536-element array allocated 如何在 AssemblyScript 中为 NEAR 合同创建 UID? - How to create a UID in AssemblyScript for a NEAR contract? 如何将ArrayBuffer从JS传递给AssemblyScript / Wasm? - How can I pass an ArrayBuffer from JS to AssemblyScript/Wasm? 如何测试 function 是否在 NEAR 智能合约(AssemblyScript)中断言? - How to test if a function asserts in a NEAR smart contract (AssemblyScript)? 从 Near 区块链调用 Aurora 合约时如何在 AssemblyScript 中编码参数? - How to encode arguments in AssemblyScript when calling Aurora contract from Near blockchain? AssemblyScript:动态本地数组大小 - AssemblyScript: Dynamic local array size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM