Java Reference
In-Depth Information
All typed array objects have the following properties:
length : It is the number of elements in the array
byteLength : It is the length of the array in bytes
buffer : It is the reference of the underlying ArrayBuffer object
used by the typed array
byteOffset : It is the offset in bytes from the start of its
ArrayBuffer
Once you create a typed array, you can use it as a simple array object. You can set
and read its elements using the bracket notation using indexes. If you set a value that is
not of the type of the typed array type, the value is converted appropriately. For example,
setting 23.56 to an element in an Int8Array will set 23 as the value. The following code
shows how to read and write contents of a typed array:
// Create an Int8Array of 3 elements. Each element is an 8-bit sign integer.
var ids = new Int8Array(3);
// Populate the array
ids[0] = 10;
ids[1] = 20.89; // 20 will be stored
ids[2] = 140; // -116 is stored as byte's range is -128 to 127.
// Read the elements
print("ids[0] = " + ids[0]);
print("ids[1] = " + ids[1]);
print("ids[2] = " + ids[2]);
ids[0] = 10
ids[1] = 20
ids[2] = -116
The following code creates an Int32Array from an Array object and reads all
elements:
// An Array object
var ids = [10, 20, 30];
// Create an Int32Array from ids
var typedIds = new Int32Array(ids);
// Read elements from typedids
for(var i = 0, len = typedIds.length; i < len; i++) {
print("typedIds[" + i + "] = " + typedIds[i]);
}
Search WWH ::




Custom Search