Share on Google+Share on Google+

Transfer the content of a float buffer into float array.

Advertisement
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

Advertisements

Posted on: July 30, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.