Home Tutorial Java Corejava Nio ShortBuffer in java, Write a short value into short buffer at given index.

 
 

ShortBuffer in java, Write a short value into short buffer at given index.
Posted on: August 12, 2010 at 12:00 AM
Advertisement
In this tutorial, you will see how to write a short value into short buffer at given index.

Write a short value into short buffer at given index.

 In this tutorial, we will see how to write the given short value into short buffer at the given index.

ShortBuffer API:

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

Return type Method Description
static ShortBuffer wrap(short[] array)  The wrap(...) method create a short buffer by wrapping  the associated short array. 
abstract  ShortBuffer put(int index, short value) The put(..) method write the given short value into associated buffer at given index.
abstract short get( ) The get(...) method returns short value from current position .

Code

import java.nio.*;
import java.nio.ShortBuffer;
public class PutValueAtIndex {
  public static final int index=2;
  public static void main(String[] arg) {
short[] array = new short[] { 1234};
ShortBuffer shortBuf = ShortBuffer.wrap(array);
System.out.println("Content in short buffer.");
    while (shortBuf.hasRemaining())
    {
    System.out.print(shortBuf.get()+" ");    
    }
    System.out.println();
System.out.println("Put value at index . "
                                      +index
);
System.out.println("Content in short buffer.");
    shortBuf.put(2(short655);
    shortBuf.flip();
    while (shortBuf.hasRemaining())
    {
    System.out.print(shortBuf.get()+" ");  
    }
  }
}

Output

C:\>java PutValueAtIndex
Content in short buffer.
1 2 3 4 5
Put value at index . 2
Content in short buffer.
1 2 655 4 5

Download this code

Advertisement

Related Tags for ShortBuffer in java, Write a short value into short buffer at given index.:


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.

Posted on: August 12, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com