DoubleBuffer array() method in Java With Examples Last Updated : 04 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The array() method of java.nio.DoubleBuffer Class is used to Return the double array that backs this buffer. Modifications to this buffer’s content will cause the returned array’s content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure that this buffer has an accessible backing array Syntax : public final float[] array() Return Value: This method returns the array that backs this buffer. Exception: This method throws the ReadOnlyBufferException if this buffer is backed by an array but is read-only. Below program illustrates the array() method: Examples 1: Java // Java program to demonstrate // array() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the DoubleBuffer int capacity = 10; // Creating the DoubleBuffer try { // creating object of Doublebuffer // and allocating size capacity DoubleBuffer db = DoubleBuffer.allocate(capacity); // putting the value in Doublebuffer db.put(8.56F); db.put(2, 9.61F); db.rewind(); // getting array from db DoubleBuffer using array() method double[] dbb = db.array(); // printing the DoubleBuffer db System.out.println("DoubleBuffer: " + Arrays.toString(dbb)); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } Output: DoubleBuffer: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] Example 2: Java // Java program to demonstrate // array() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the db int capacity1 = 10; // Declaring the capacity of the db1 int capacity2 = 5; // Creating the DoubleBuffer try { // // db // // creating object of Doublebuffer db // and allocating size capacity DoubleBuffer db = DoubleBuffer.allocate(capacity1); // putting the value in db db.put(9.56F); db.put(2, 7.61F); db.put(3, 4.61F); db.rewind(); // print the DoubleBuffer System.out.println("DoubleBuffer db: " + Arrays.toString(db.array())); // // db1 // // creating object of Doublebuffer db1 // and allocating size capacity DoubleBuffer db1 = DoubleBuffer.allocate(capacity2); // putting the value in db1 db1.put(1, 4.56F); db1.put(2, 6.45F); db1.rewind(); // print the DoubleBuffer System.out.println("\nDoubleBuffer db1: " + Arrays.toString(db1.array())); // Creating a read-only copy of DoubleBuffer // using asReadOnlyBuffer() method DoubleBuffer readOnlyDb = db.asReadOnlyBuffer(); // print the DoubleBuffer System.out.print("\nReadOnlyBuffer DoubleBuffer: "); while (readOnlyDb.hasRemaining()) System.out.print(readOnlyDb.get() + ", "); // try to change readOnlyDb System.out.println("\n\nTrying to get the array" + " from ReadOnlyDb for editing"); Arrays.toString(readOnlyDb.array()); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("Exception thrown: " + e); } } } Output: DoubleBuffer db: [9.5600004196167, 0.0, 7.610000133514404, 4.610000133514404, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] DoubleBuffer db1: [0.0, 4.559999942779541, 6.449999809265137, 0.0, 0.0] ReadOnlyBuffer DoubleBuffer: 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 ReadOnlyDb for editing Exception thrown: java.nio.ReadOnlyBufferException Comment More infoAdvertise with us Next Article DoubleBuffer array() method in Java With Examples pawan_asipu Follow Improve Article Tags : Misc Java Java-Functions Java-NIO package Java-DoubleBuffer +1 More Practice Tags : JavaMisc Similar Reads DoubleBuffer arrayOffset() method in Java With Examples The arrayOffset() method of java.nio.DoubleBuffer class is used to return the offset within the bufferâs backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). Inorder to check whether 3 min read ByteBuffer array() method in Java with Examples The array() method of java.nio.ByteBuffer class is used to return the byte array that backs the taken buffer.Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.Invoke the hasArray() method before invoking this method in order to ensure that 3 min read DoubleBuffer clear() methods in Java with Examples The clear() method of java.nio.CharBuffer Class is used to clear this buffer. While clearing this buffer following changes are done: the position is set to zero the limit is set to the capacity the mark is discarded. Syntax: public final DoubleBuffer clear() Return Value: This method returns this Do 2 min read Buffer array() methods in Java with Examples The array() method of java.nio.Buffer class is used to return the array that backs the taken buffer. This method is intended to allow array-backed buffers to be passed to native code more efficiently. Concrete subclasses provide more strongly-typed return values for this method. Modifications to thi 3 min read DoubleBuffer allocate() method in Java With Examples The allocate() method of java.nio.DoubleBuffer Class is used to allocate a new double buffer next to the existing buffer. The new bufferâs position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing a 2 min read DoubleBuffer compact() method in Java With Examples The compact() method of java.nio.DoubleBuffer Class is used to compact the given buffer. The values between the bufferâs current position and its limit are copied to the beginning of the buffer. The bufferâs position is then set to n+1 and its limit is set to its capacity. The bufferâs position is s 3 min read DoubleBuffer compareTo() method in Java With Examples The compareTo() method of java.nio.DoubleBuffer class is used to compare one buffer to another. Two double buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of flo 3 min read Buffer arrayOffset() method in Java with Examples The arrayOffset() method of java.nio.Buffer class is used to return the offset within the given buffer's backing array of the first element of the buffer. If this buffer is backed by an array then buffer position p corresponds to array index p + arrayOffset(). Invoke the hasArray method before invok 3 min read DoubleBuffer wrap() method in Java with Examples wrap(double[] array) The wrap() method of java.nio.DoubleBuffer Class is used to wraps a double array into a buffer. The new buffer will be backed by the given double array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new bufferâs capacity and limit w 4 min read DoubleBuffer get() methods in Java with Examples The get() method of java.nio.DoubleBuffer Class is used to reads the double at the given bufferâs current position, and then increments the position. Syntax: public abstract double get() Return Value: This method returns the double value at the bufferâs current position. Exception: This method throw 3 min read Like