FloatBuffer array() method in Java With Examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The array() method of java.nio.FloatBuffer Class is used to Return the float 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. Throws: 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 FloatBuffer int capacity = 10; // Creating the FloatBuffer try { // creating object of floatbuffer // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity); // putting the value in floatbuffer fb.put(8.56F); fb.put(2, 9.61F); fb.rewind(); // getting array from fb FloatBuffer using array() method float[] fbb = fb.array(); // printing the FloatBuffer fb System.out.println("FloatBuffer: " + Arrays.toString(fbb)); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } Output: FloatBuffer: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] Examples 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 fb int capacity1 = 10; // Declaring the capacity of the fb1 int capacity2 = 5; // Creating the FloatBuffer try { // // fb // // creating object of floatbuffer fb // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity1); // putting the value in fb fb.put(9.56F); fb.put(2, 7.61F); fb.put(3, 4.61F); fb.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb: " + Arrays.toString(fb.array())); // // fb1 // // creating object of floatbuffer fb1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity2); // putting the value in fb1 fb1.put(1, 4.56F); fb1.put(2, 6.45F); fb1.rewind(); // print the FloatBuffer System.out.println("\nFloatBuffer fb1: " + Arrays.toString(fb1.array())); // Creating a read-only copy of FloatBuffer // using asReadOnlyBuffer() method FloatBuffer readOnlyFb = fb.asReadOnlyBuffer(); // print the FloatBuffer System.out.print("\nReadOnlyBuffer FloatBuffer: "); while (readOnlyFb.hasRemaining()) System.out.print(readOnlyFb.get() + ", "); // try to change readOnlyFb System.out.println("\n\nTrying to get the array" + " from ReadOnlyFb for editing"); float[] fbarr = readOnlyFb.array(); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("Exception thrown: " + e); } } } Output: FloatBuffer fb: [9.56, 0.0, 7.61, 4.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] FloatBuffer fb1: [0.0, 4.56, 6.45, 0.0, 0.0] ReadOnlyBuffer FloatBuffer: 9.56, 0.0, 7.61, 4.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Trying to get the array from ReadOnlyFb for editing Exception thrown: java.nio.ReadOnlyBufferException Comment More infoAdvertise with us Next Article FloatBuffer array() method in Java With Examples rohitprasad3 Follow Improve Article Tags : Java Java Programs Java - util package java-basics Java-Functions Java 8 Java-FloatBuffer Java-NIO package +4 More Practice Tags : Java Similar Reads How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co 2 min read Simplest Method to Print Array in Java Arrays.toString() Method of java.util.Arrays class is the simplest method to print an array in Java. This method takes an array as a parameter and returns a string representation of the array and it can work with all types of arrays like integer arrays, string arrays, etc.Example:Java// Java Program 1 min read Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem 6 min read How to Fill (initialize at once) an Array in Java? An array is a group of like-typed variables that are referred to by a common name. In this, article we will learn about Filling array in Java while Initialization.Example:Java// Java program to fill the element in an array import java.util.*; public class Geeks { public static void main(String args[ 3 min read Program to convert Primitive Array to Stream in Java An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case 3 min read How to Convert an ArrayList Containing Integers to Primitive Int Array? In Java, ArrayList is the pre-defined class of the Java Collection Framework. It is part of the java.util package. ArrayList can be used to add or remove an element dynamically in the Java program. It can be snipped dynamically based on the elements added or removed into the ArrayList. In this artic 2 min read Java Program to Illustrate the Usage of Floating The floating-point number are those number which needs fractional precision that is the number that can be in the fraction. There many more mathematical calculations in which floating type is involved. For e.g. finding the square root of a number up to certain decimal values finding the cube root of 3 min read Java Array Programs An array is a data structure consisting of a collection of elements (values or variables), of the same memory size, each identified by at least one array index or key. An array is a linear data structure that stores similar elements (i.e. elements of similar data type) that are stored in contiguous 4 min read FloatBuffer arrayOffset() method in Java With Examples The arrayOffset() method of java.nio.FloatBuffer 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 FloatBuffer allocate() method in Java With Examples The allocate() method of java.nio.FloatBuffer Class is used to allocate a new float 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 arr 2 min read Like