xxxxxxxxxx
<script>
//Create a 16byte buffer
var buffer = new ArrayBuffer(16);
//Create a DataView referring to the buffer
var view1 = new DataView(buffer);
//Create a Int8Array view referring to the buffer
var view2 = new Int8Array(buffer);
//Put value of 32bits
view1.setInt32(0, 0x76543210);
//prints the 32bit value
document.write(view1.getInt32(0).toString(16) + "<br>");
//prints only 8bit value
document.write(view1.getInt8(0).toString(16) + "<br>");
document.write(view2[0].toString(16));
</script>
xxxxxxxxxx
// Create an ArrayBuffer with a length of 8 bytes
const buffer = new ArrayBuffer(8);
// Create a view (TypedArray) to work with the ArrayBuffer
const view = new Int32Array(buffer);
// Write some data to the ArrayBuffer through the view
view[0] = 42;
view[1] = 100;
// Read data from the ArrayBuffer through the view
console.log(view[0]); // Outputs: 42
console.log(view[1]); // Outputs: 100
Introduction
Creating an ArrayBuffer
Adding Elements
Deleting Elements
-= Assignment Operator
remove method
clear method
Introduction
An ArrayBuffer in Scala is a collection that comes under the sequence class. Like Arrays, it is a mutable collection, hence, when elements in an ArrayBuffer are modified, the original ArrayBuffer is updated.
ArrayBuffers are very similar to arrays with the difference that you can add and remove elements from an ArrayBuffer while adding and removing elements is not possible in simple Arrays.
Array methods and operations are also available for ArrayBuffers.
Creating an ArrayBuffer#
To be able to use an ArrayBuffer, we first need to import it using the package scala.collection.mutable.ArrayBuffer. Afterward, you can create and populate an ArrayBuffer the same way you create an Array.
xxxxxxxxxx
import scala.collection.mutable.ArrayBuffer
val myFirstArrayBuffer = ArrayBuffer(1,2,3,4,5)
// Driver Code
myFirstArrayBuffer.foreach(println)