Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

Whats is the DoubleBuffer.array() method in Java?

Muhammad Ashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The DoubleBuffer.array() method is part of the DoubleBuffer class and returns the double array that backs up the buffer. If the contents of the buffer are modified, then that is reflected in the array as well, and vice versa. It is recommended to invoke the hasArray method before invoking DoubleBuffer.array() in order to ensure that this buffer has an accessible backing array.

Prototype

public final double[] array()

Parameters

None.

Return value

array() returns the array that backs the buffer.

Exception

array() throws ReadOnlyBufferException if the buffer is backed by an array but is read-only. It throws UnsupportedOperationException if the buffer is not backed by an accessible array.

Code

import java.nio.*;
import java.util.*;
class HelloWorld {
public static void main( String args[] ) {
double [] dummy = {1.2, 3.0, 9.0};
DoubleBuffer db = DoubleBuffer.wrap(dummy);
double [] arr = db.array();
System.out.println( "Before: " + Arrays.toString( arr ));
db.put(100.0F);
System.out.println( "After: " + Arrays.toString( arr ));
}
}

In this code snippet, we create a DoubleBuffer by wrapping a double array using the wrap() method.

We return the array backing up this buffer using the array() method and store it in the variable arr. We then print the contents of arr to the standard output.

In the next line, we use the put method to modify the contents of the buffer and print the contents of the arr array to show that the changes made to the buffer were directly reflected in the array as well.

RELATED TAGS

java

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring