Allocates a IntBuffer with the given length. - Java java.nio

Java examples for java.nio:IntBuffer

Description

Allocates a IntBuffer with the given length.

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.nio.IntBuffer;

public class Main {
    public static void main(String[] argv) throws Exception {
        int length = 2;
        System.out.println(allocateIntBuffer(length));
    }//www . j a va2s .c  o  m

    /**
     * Allocates a IntBuffer with the given length.
     *
     * @param length The length of the buffer to allocate (in elements, not bytes!).
     * @return The newly allocated buffer.
     */
    public static IntBuffer allocateIntBuffer(int length) {
        return ByteBuffer.allocateDirect(length * 4) // int == 4 bytes
                .order(ByteOrder.nativeOrder()).asIntBuffer();
    }
}

Related Tutorials