Home Tutorial Java Corejava Nio Transfer the content of a float buffer into float array.

 
 

Transfer the content of a float buffer into float array.
Posted on: July 30, 2010 at 12:00 AM
In this tutorial you will see how to transfer the content of a float buffer into float array.

Transfer the content of a float buffer into float array.

 In this tutorial, we will see how to transfer the content of a float buffer into float array.

FloatBufferAPI:

The java.nio.FloatBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
static FloatBuffer allocate(int capacity)  The allocate(..)method allocate a new float buffer.
 FloatBuffer get(float array) The get(..)method transfer the content of a float buffer into float array.

code

import java.nio.*;
import java.nio.FloatBuffer;

public class BufferToArray {
public static void main(String[] args) {
   try {
FloatBuffer floatBuf1 = FloatBuffer.allocate(1024);
      floatBuf1.put(12.98f);
      floatBuf1.put(54.4f);
      floatBuf1.put(78.94f);
      floatBuf1.put(87.56f);
      float[] s = new float[4];
      floatBuf1.flip();
      floatBuf1.get(s);
  System.out.println("Content of a float array.");
      for (int i = 0; i < s.length; i++) {
        System.out.println(s[i]);
      }
    catch (Exception e) {
      System.out.println(e);
    }
  }
}

Output

C:\>java BufferToArray
Content of a float array.
12.98
54.4
78.94
87.56

Download this code

Related Tags for Transfer the content of a float buffer into float array.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.