Java的.nio.IntBuffer类在Java中
IntBuffer保存要在 I/O 操作中使用的整数值序列。 IntBuffer类对长缓冲区提供以下四类操作:
- 读取单个整数的绝对和相对获取方法。
- 写入单个整数的绝对和相对放置方法。
- 相对批量 put 和 get 方法将连续的 int 序列从 int 数组或其他一些 int 缓冲区传输到此缓冲区,并从此缓冲区传输到数组。
可以通过以下方式创建 Int 缓冲区:
- 分配():这为缓冲区的内容分配空间。
- wrap():将现有的长数组包装到缓冲区中。
IntBuffer 类的大部分方法都直接类似于 ByteBuffer 定义的方法。
类声明:
public abstract class IntBuffer
extends Buffer
implements Comparable
IntBuffer 类的方法: Method Description allocate(int capacity) This method allocates a new int buffer. array() This method returns the int array that backs this buffer. arrayOffset() This method returns the offset within this buffer’s backing array of the first element of the buffer. asReadOnlyBuffer() This method creates a new, read-only int buffer that shares this buffer’s content. compact() This method compacts this buffer. compareTo(IntBuffer that) This method compares this buffer to another. duplicate() This method creates a new int buffer that shares this buffer’s content. equals(Object ob) This method tells whether this buffer is equal to another object. get() This method is a relative get method and returns the int at the buffer’s current position. get(int index) This method is an absolute get method and returns the int at the given index. get(int[] dst) This method is a relative bulk get method and returns this buffer. get(int[] dst, int offset, int length) This method relative is a bulk get method and returns this buffer. hasArray() This method tells whether this buffer is backed by an accessible int array. hashCode() This method returns the current hash code of this buffer. isDirect() This method tells whether this int buffer is direct. order() This method retrieves this buffer’s byte order. put(int i) This method is a relative put method and returns this buffer. put(int[] src) This method is a relative bulk put method and returns this buffer. put(int[] src, int offset, int length) This method is a relative bulk put method and returns this buffer. put(IntBuffer src) This method is a relative bulk put method and returns this buffer. put(int index, int i) This method is an absolute bulk put method and returns this buffer. slice() This method creates a new int buffer whose content is a shared subsequence of this buffer’s content. toString() This method returns a string summarizing the state of this buffer. wrap(int[] array) This method wraps an int array into a buffer. wrap(int[] array, int offset, int length) This method wraps an int array into a buffer.
以下是一些演示 IntBuffer 类及其方法的程序:
示例 1:
Java
// Java program to demonstrate
// IntBuffer class
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int Capacity = 10;
// Creating the IntBuffer
// creating object of intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(Capacity);
// putting the value in intbuffer
ib.put(100);
ib.put(2, 9);
System.out.println("IntBuffer: "
+ Arrays.toString(ib.array()));
}
}
Java
// Java program to demonstrate
// IntBuffer class
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 10;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
ib.put(4);
ib.put(2, 9);
ib.rewind();
// checking IntBuffer ib is backed by array or
// not
boolean isArray = ib.hasArray();
// checking if else condition
if (isArray)
System.out.println("IntBuffer ib is"
+ " backed by array");
else
System.out.println(
"IntBuffer ib is"
+ " not backed by any array");
}
catch (IllegalArgumentException e) {
System.out.println(
"IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println(
"ReadOnlyBufferException catched");
}
}
}
IntBuffer: [100, 0, 9, 0, 0, 0, 0, 0, 0, 0]
示例 2:
Java
// Java program to demonstrate
// IntBuffer class
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 10;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
ib.put(4);
ib.put(2, 9);
ib.rewind();
// checking IntBuffer ib is backed by array or
// not
boolean isArray = ib.hasArray();
// checking if else condition
if (isArray)
System.out.println("IntBuffer ib is"
+ " backed by array");
else
System.out.println(
"IntBuffer ib is"
+ " not backed by any array");
}
catch (IllegalArgumentException e) {
System.out.println(
"IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println(
"ReadOnlyBufferException catched");
}
}
}
IntBuffer ib is backed by array