DoubleBuffer array() method in Java With ExamplesThe java.nio.DoubleBuffer has array() method. The double array with support for this buffer is returned using the DoubleBuffer Class. Changes made to the contents of this buffer will also change the contents of the returned array. Before calling this function, invoke the hasArray() method to make sure this buffer has an available backing array. As it precisely establishes limits for the operations that follow, the flip() function is essential for converting from writing mode to reading mode. A buffer's capacity to access the underlying storage directly while there is a backing array present can be helpful, too, but it also needs to be handled carefully, mainly if the array size is different from the number of valid elements in the buffer. Syntax: Return Value: The array backing up this buffer is returned by this function. Exception: If this buffer has an array behind it but is read-only, this function throws the ReadOnlyBufferException. Example 1:The method illustrates how changes in one reflect in the other, indicating synchronization between the buffer and its underlying array. However, as these may not have been explicitly filled with valid values, consideration must be taken when accessing array elements beyond the current limit specified by the buffer. Next, the limit is set to the current position (which indicates how many items have been added), and the position is reset to zero using the flip() method, which prepares the buffer for reading. Implementation:FileName: DoubleArrayExample1.java Output: The elements in the array: 11.1 22.2 33.3 44.4 0.0 0.0 Example 2:The code shows how to work with DoubleBuffer objects in Java, which are buffers that can handle double-precision floating-point values.The backing arrays of these buffers are accessed and printed using array() to display the internal storage. Moreover, the code generates a read-only view of doublebuff by calling the asReadOnlyBuffer() method to prevent modifications. Accessing the backing array of this read-only buffer results in a ReadOnlyBufferException, indicating that the read-only buffer enforces immutability. Finally, the code handles exceptions for IllegalArgumentException and ReadOnlyBufferException to illustrate error scenarios related to buffer operations. Implementation:FileName: DoubleBufferExample2.java Output: The DoubleBuffer is given by : [9.5600004196167, 0.0, 7.610000133514404, 4.610000133514404, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] The DoubleBuffer doublebuff1 is given by : [0.0, 2.359999895095825, 5.389999866485596, 0.0, 0.0] The ReadOnlyBuffer DoubleBuffer is given by : 9.5600004196167, 0.0, 7.610000133514404, 4.610000133514404, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Trying to get the array from ReadOnly for editing The Exception thrown is: java.nio.ReadOnlyBufferException |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India