How to get given index value from FloatBuffer in java.

In this tutorial you will see how to get given index value from FloatBuffer in java.

 
 

Share on Google+Share on Google+

How to get given index value from FloatBuffer in java.

Ads

In this tutorial you will see how to get given index value from FloatBuffer in java.

How to get given index value from FloatBuffer in java.

 In this tutorial, we will discuss how to get given index value from FloatBuffer in java.

FloatBuffer API:

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 create a new float buffer of  given capacity. 
abstract float get() The get(...) method returns float value at current position and increment the position by 1.
abstract float get(int index) The get(...) method returns float value at given index.

Code

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

public class IndexValueDemo {
public static void main(String[] args) {
    int index = 1;
FloatBuffer floatBuf = FloatBuffer.allocate(256);
    floatBuf.put(4.2f);
    floatBuf.put(5.1f);
    floatBuf.put(6.4f);
    floatBuf.flip();
System.out.println("All values in float buffer.");
    while (floatBuf.hasRemaining()) {
      System.out.println(floatBuf.get());
    }
System.out.println("Value in float buffer at given index.");
    System.out.println(floatBuf.get(index));
  }
}

Output

C:\>java IndexValueDemo
All values in float buffer.
4.2
5.1
6.4
Value in float buffer at given index.
5.1

Download this code

Advertisements

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