Java : IntBuffer 示例
Java 中的 IntBuffer (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 IntBuffer 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0
buffer.put(10);
System.out.println(buffer.position()); // 1
buffer.put(20);
System.out.println(buffer.position()); // 2
buffer.put(30);
System.out.println(buffer.position()); // 3
if (buffer.hasArray()) {
final var ret = buffer.array();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 0, 0]
}
final int[] array = {10, 20, 30};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 3
System.out.println(buffer.limit()); // 3
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3
Methods
static IntBuffer allocate (int capacity)
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0, 0, 0, 0, 0]
}
final int[] array ()
final var buffer = IntBuffer.allocate(5)
.put(10)
.put(20)
.put(30);
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
final int arrayOffset ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
final var sliced = buffer.slice(2, 3);
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
if (buffer.hasArray()) {
System.out.println(buffer.arrayOffset()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
}
System.out.println(sliced); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=3]
if (sliced.hasArray()) {
System.out.println(sliced.arrayOffset()); // 2
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.get()); // 50
}
abstract IntBuffer asReadOnlyBuffer ()
final var buffer = IntBuffer.allocate(3);
buffer.put(10);
final var readOnlyBuffer = buffer.asReadOnlyBuffer();
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=1 lim=3 cap=3]
System.out.println(readOnlyBuffer); // java.nio.HeapIntBufferR[pos=1 lim=3 cap=3]
readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 10
System.out.println(readOnlyBuffer.get()); // 0
System.out.println(readOnlyBuffer.get()); // 0
buffer.put(20);
readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 10
System.out.println(readOnlyBuffer.get()); // 20
System.out.println(readOnlyBuffer.get()); // 0
try {
var _ = readOnlyBuffer.put(30);
} catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException!");
}
// Result
// ↓
//ReadOnlyBufferException!
final IntBuffer clear ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
buffer.position(2);
buffer.limit(4);
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=2 lim=4 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.clear()); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false
abstract IntBuffer compact ()
final int[] array = {10, 20, 30, 40, 50, 60};
final var buffer = IntBuffer.wrap(array);
buffer.position(3);
System.out.println(buffer.compact()); // java.nio.HeapIntBuffer[pos=3 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [40, 50, 60, 40, 50, 60]
final int[] array = {10, 20, 30, 40, 50, 60};
final var buffer = IntBuffer.wrap(array);
buffer.position(2);
System.out.println(buffer.compact()); // java.nio.HeapIntBuffer[pos=4 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [30, 40, 50, 60, 50, 60]
final int[] array = {10, 20, 30, 40, 50, 60};
final var buffer = IntBuffer.wrap(array);
buffer.position(4);
System.out.println(buffer.compact()); // java.nio.HeapIntBuffer[pos=2 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [50, 60, 30, 40, 50, 60]
int compareTo (IntBuffer that)
final var buffer1 = IntBuffer.allocate(3)
.put(10)
.put(20)
.put(30);
final var buffer2 = IntBuffer.allocate(3)
.put(10)
.put(20)
.put(30);
buffer1.clear();
buffer2.clear();
System.out.println(buffer1.compareTo(buffer2)); // 0
final var buffer1 = IntBuffer.allocate(3)
.put(10)
.put(20)
.put(30);
final var buffer2 = IntBuffer.allocate(3)
.put(5)
.put(50)
.put(500);
final var buffer3 = IntBuffer.allocate(3)
.put(30)
.put(20)
.put(10);
buffer1.clear();
buffer2.clear();
buffer3.clear();
System.out.println(buffer1.compareTo(buffer2)); // 1
System.out.println(buffer1.compareTo(buffer3)); // -1
abstract IntBuffer duplicate ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
buffer.position(1);
buffer.limit(4);
final var duplicated = buffer.duplicate();
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=1 lim=4 cap=5]
System.out.println(duplicated); // java.nio.HeapIntBuffer[pos=1 lim=4 cap=5]
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(duplicated.get()); // 20
System.out.println(duplicated.get()); // 30
System.out.println(duplicated.get()); // 40
boolean equals (Object ob)
final var buffer1 = IntBuffer.allocate(3)
.put(10)
.put(20)
.put(30);
final var buffer2 = IntBuffer.allocate(3)
.put(10)
.put(20)
.put(30);
buffer1.clear();
buffer2.clear();
System.out.println(buffer1.equals(buffer2)); // true
final var buffer1 = IntBuffer.allocate(3)
.put(10)
.put(20)
.put(30);
final var buffer2 = IntBuffer.allocate(3)
.put(5)
.put(50)
.put(500);
final var buffer3 = IntBuffer.allocate(3)
.put(30)
.put(20)
.put(10);
buffer1.clear();
buffer2.clear();
buffer3.clear();
System.out.println(buffer1.equals(buffer2)); // false
System.out.println(buffer1.equals(buffer3)); // false
final IntBuffer flip ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.flip()); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
abstract int get ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
abstract int get (int index)
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
System.out.println(buffer.get(0)); // 10
System.out.println(buffer.get(1)); // 20
System.out.println(buffer.get(2)); // 30
System.out.println(buffer.get(3)); // 40
System.out.println(buffer.get(4)); // 50
System.out.println(buffer.position()); // 0
IntBuffer get (int[] dst)
final int[] array = {10, 20, 30, 40, 50, 60, 70};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 7
System.out.println(buffer.position()); // 0
{
final var dst = new int[3];
System.out.println(Arrays.toString(dst)); // [0, 0, 0]
final var ret = buffer.get(dst);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=3 lim=7 cap=7]
System.out.println(Arrays.toString(dst)); // [10, 20, 30]
System.out.println(buffer.position()); // 3
}
{
final var dst = new int[3];
System.out.println(Arrays.toString(dst)); // [0, 0, 0]
final var ret = buffer.get(dst);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=6 lim=7 cap=7]
System.out.println(Arrays.toString(dst)); // [40, 50, 60]
System.out.println(buffer.position()); // 6
}
try {
final var dst = new int[3];
var _ = buffer.get(dst);
} catch (BufferUnderflowException e) {
System.out.println("BufferUnderflowException!");
}
// Result
// ↓
//BufferUnderflowException!
IntBuffer get (int[] dst, int offset, int length)
final int[] array = {10, 20, 30, 40, 50, 60, 70, 80};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 8
System.out.println(buffer.position()); // 0
{
final var dst = new int[5];
System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]
final var ret = buffer.get(dst, 0, 3);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=3 lim=8 cap=8]
System.out.println(Arrays.toString(dst)); // [10, 20, 30, 0, 0]
System.out.println(buffer.position()); // 3
}
{
final var dst = new int[5];
System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]
final var ret = buffer.get(dst, 1, 3);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=6 lim=8 cap=8]
System.out.println(Arrays.toString(dst)); // [0, 40, 50, 60, 0]
System.out.println(buffer.position()); // 6
}
{
final var dst = new int[5];
System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]
final var ret = buffer.get(dst, 3, 2);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=8 lim=8 cap=8]
System.out.println(Arrays.toString(dst)); // [0, 0, 0, 70, 80]
System.out.println(buffer.position()); // 8
}
IntBuffer get (int index, int[] dst)
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
{
final var dst = new int[3];
System.out.println(Arrays.toString(dst)); // [0, 0, 0]
final var ret = buffer.get(0, dst);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(Arrays.toString(dst)); // [10, 20, 30]
System.out.println(buffer.position()); // 0
}
{
final var dst = new int[3];
System.out.println(Arrays.toString(dst)); // [0, 0, 0]
final var ret = buffer.get(1, dst);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(Arrays.toString(dst)); // [20, 30, 40]
System.out.println(buffer.position()); // 0
}
{
final var dst = new int[3];
System.out.println(Arrays.toString(dst)); // [0, 0, 0]
final var ret = buffer.get(2, dst);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(Arrays.toString(dst)); // [30, 40, 50]
System.out.println(buffer.position()); // 0
}
IntBuffer get (int index, int[] dst, int offset, int length)
final int[] array = {10, 20, 30, 40, 50, 60};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
{
final var dst = new int[5];
System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]
final var ret = buffer.get(0, dst, 0, 3);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(dst)); // [10, 20, 30, 0, 0]
System.out.println(buffer.position()); // 0
}
{
final var dst = new int[5];
System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]
final var ret = buffer.get(2, dst, 1, 3);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(dst)); // [0, 30, 40, 50, 0]
System.out.println(buffer.position()); // 0
}
{
final var dst = new int[5];
System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]
final var ret = buffer.get(4, dst, 3, 2);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(dst)); // [0, 0, 0, 50, 60]
System.out.println(buffer.position()); // 0
}
final boolean hasArray ()
final var buffer = IntBuffer.allocate(5)
.put(10)
.put(20)
.put(30);
final var ret = buffer.hasArray();
System.out.println(ret); // true
if (ret) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
final var buffer = IntBuffer.allocate(5)
.put(10)
.put(20)
.put(30)
.asReadOnlyBuffer();
System.out.println(buffer.hasArray()); // false
try {
var _ = buffer.array();
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
int hashCode ()
final var buffer = IntBuffer.allocate(0);
System.out.println(buffer.hashCode()); // 1
final var buffer = IntBuffer.allocate(3)
.put(10)
.put(20)
.put(30)
.clear();
System.out.println(buffer.hashCode()); // 59251
final var buffer = IntBuffer.allocate(3)
.put(-10)
.put(-20)
.put(-30)
.clear();
System.out.println(buffer.hashCode()); // 331
abstract boolean isDirect ()
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final int[] array = {10, 20, 30};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.isDirect()); // false
final var buffer = ByteBuffer.allocateDirect(12).asIntBuffer();
System.out.println(buffer.isDirect()); // true
final IntBuffer limit (int newLimit)
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
System.out.println(buffer.limit(3)); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3
System.out.println(buffer.clear()); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.limit(3)); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
try {
var _ = buffer.get();
} catch (BufferUnderflowException e) {
System.out.println("BufferUnderflowException!");
}
// Result
// ↓
//BufferUnderflowException!
final IntBuffer mark ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapIntBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapIntBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
int mismatch (IntBuffer that)
final int[] array = {10, 20, 30, 40};
final var buffer = IntBuffer.wrap(array);
{
final int[] a = {10};
final var that = IntBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 1
}
{
final int[] a = {10, 20};
final var that = IntBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 2
}
{
final int[] a = {10, 20, 30};
final var that = IntBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 3
}
{
final int[] a = {10, 20, 30, 40};
final var that = IntBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // -1
}
{
final int[] a = {10, 20, 30, 40, 50};
final var that = IntBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 4
}
{
final int[] a = {10, 20, 999};
final var that = IntBuffer.wrap(a); // 2
System.out.println(buffer.mismatch(that));
}
{
final int[] a = {-90, -80};
final var that = IntBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 0
}
abstract ByteOrder order ()
final var os = System.getProperty("os.name");
System.out.println(os); // Windows 10
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer.order()); // LITTLE_ENDIAN
final IntBuffer position (int newPosition)
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3
System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
System.out.println(buffer.position(3)); // java.nio.HeapIntBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
abstract IntBuffer put (int i)
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
System.out.println(buffer.put(10)); // java.nio.HeapIntBuffer[pos=1 lim=5 cap=5]
System.out.println(buffer.put(20)); // java.nio.HeapIntBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.put(30)); // java.nio.HeapIntBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.put(40)); // java.nio.HeapIntBuffer[pos=4 lim=5 cap=5]
System.out.println(buffer.put(50)); // java.nio.HeapIntBuffer[pos=5 lim=5 cap=5]
System.out.println(buffer.position()); // 5
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}
final IntBuffer put (int[] src)
final var buffer = IntBuffer.allocate(6);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
{
final int[] src = {10};
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=1 lim=6 cap=6]
}
{
final int[] src = {20, 30};
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=3 lim=6 cap=6]
}
{
final int[] src = {40, 50, 60};
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=6 lim=6 cap=6]
}
System.out.println(buffer.position()); // 6
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
}
IntBuffer put (int[] src, int offset, int length)
final var array = new int[6];
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
final int[] src = {10, 20, 30, 40, 50, 60};
final var ret1 = buffer.put(src, 0, 1);
System.out.println(ret1); // java.nio.HeapIntBuffer[pos=1 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]
final var ret2 = buffer.put(src, 1, 2);
System.out.println(ret2); // java.nio.HeapIntBuffer[pos=3 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]
final var ret3 = buffer.put(src, 3, 3);
System.out.println(ret3); // java.nio.HeapIntBuffer[pos=6 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
System.out.println(buffer.position()); // 6
abstract IntBuffer put (int index, int i)
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
System.out.println(buffer.put(0, 10)); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(1, 20)); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(2, 30)); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(3, 40)); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(4, 50)); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.position()); // 0
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}
IntBuffer put (int index, int[] src)
final var buffer = IntBuffer.allocate(6);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
{
final int[] src = {10};
final var ret = buffer.put(0, src);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
}
{
final int[] src = {20, 30};
final var ret = buffer.put(1, src);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
}
{
final int[] src = {40, 50, 60};
final var ret = buffer.put(3, src);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
}
System.out.println(buffer.position()); // 0
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
}
IntBuffer put (int index, int[] src, int offset, int length)
final var array = new int[6];
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
final int[] src = {10, 20, 30, 40, 50, 60};
final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]
final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]
final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
System.out.println(buffer.position()); // 0
IntBuffer put (int index, IntBuffer src, int offset, int length)
final var array = new int[6];
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
final int[] srcArray = {10, 20, 30, 40, 50, 60};
final var src = IntBuffer.wrap(srcArray);
final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]
final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]
final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapIntBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
System.out.println(buffer.position()); // 0
IntBuffer put (IntBuffer src)
final var buffer = IntBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
{
final int[] array = {10, 20};
final var src = IntBuffer.wrap(array);
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=2 lim=5 cap=5]
}
{
final int[] array = {30, 40, 50};
final var src = IntBuffer.wrap(array);
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapIntBuffer[pos=5 lim=5 cap=5]
}
System.out.println(buffer.position()); // 5
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}
final IntBuffer reset ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapIntBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapIntBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
final IntBuffer rewind ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
buffer.position(2);
buffer.limit(4);
System.out.println(buffer.position()); // 2
System.out.println(buffer.limit()); // 4
System.out.println(buffer.rewind()); // java.nio.HeapIntBuffer[pos=0 lim=4 cap=5]
System.out.println(buffer.position()); // 0
System.out.println(buffer.limit()); // 4
abstract IntBuffer slice ()
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
buffer.position(1);
buffer.limit(4);
final var sliced = buffer.slice();
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=1 lim=4 cap=5]
System.out.println(sliced); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=3]
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false
System.out.println(sliced.get()); // 20
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.hasRemaining()); // false
abstract IntBuffer slice (int index, int length)
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
final var sliced = buffer.slice(2, 3);
System.out.println(buffer); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
System.out.println(sliced); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=3]
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.get()); // 50
System.out.println(sliced.hasRemaining()); // false
String toString ()
final var buffer = IntBuffer.allocate(5)
.limit(4)
.position(2);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 4
System.out.println(buffer.position()); // 2
final var str1 = buffer.toString();
System.out.println(str1); // java.nio.HeapIntBuffer[pos=2 lim=4 cap=5]
buffer.clear();
final var str2 = buffer.toString();
System.out.println(str2); // java.nio.HeapIntBuffer[pos=0 lim=5 cap=5]
static IntBuffer wrap (int[] array)
final int[] array = {10, 20, 30, 40, 50};
final var buffer = IntBuffer.wrap(array);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0
if (buffer.hasArray()) {
System.out.println(buffer.arrayOffset()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
}
static IntBuffer wrap (int[] array, int offset, int length)
final int[] array = {10, 20, 30, 40, 50, 60, 70};
final var buffer = IntBuffer.wrap(array, 2, 4);
System.out.println(buffer.capacity()); // 7
System.out.println(buffer.limit()); // 6
System.out.println(buffer.position()); // 2
if (buffer.hasArray()) {
System.out.println(buffer.arrayOffset()); // 0
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.get()); // 60
}