Java IntBuffer put()方法
put(int i)
java.nio.IntBuffer 类的 put(int i) 方法用于将给定的int写入新创建的int缓冲区的当前位置,然后递增该位置。
语法:
public abstract IntBuffer put(int i)
Java
参数: 该方法以int值i为参数,将其写入int缓冲区。
返回值: 该方法返回这个缓冲区,其中插入了int值。
异常: 该方法会抛出以下异常。
- BufferOverflowException- 如果这个缓冲区的当前位置不小于它的极限值。
- ReadOnlyBufferException- 如果这个缓冲区是只读的。
下面是说明put(int i)方法的例子。
例1 :
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer using put() method
ib.put(8);
ib.put(9);
ib.put(7);
ib.rewind();
// print the IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Java
输出:
Original IntBuffer: [8, 9, 7]
Java
例2: 为了证明BufferOverflowException。
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer using put() method
ib.put(8);
ib.put(9);
ib.put(7);
System.out.println("Trying to put the Int at the "
+ "position more than its limit");
ib.put(7);
// rewind the Intbuffer
ib.rewind();
// print the IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Java
输出:
Trying to put the Int at the position more than its limit
Exception throws : java.nio.BufferOverflowException
Java
实例3: 为了证明ReadOnlyBufferException。
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity using allocate() method
IntBuffer ib = IntBuffer.allocate(capacity);
// Creating a read-only copy of IntBuffer
// using asReadOnlyBuffer() method
IntBuffer ib1 = ib.asReadOnlyBuffer();
System.out.println("Trying to put the Int value"
+ " in read only buffer");
// putting the value in readonly Intbuffer
// using put() method
ib1.put(8);
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Java
输出:
Trying to put the Int value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
Java
put(int index, int i)
java.nio.IntBuffer类的put(int index, int i)方法是用来将给定的int写入给定索引的缓冲区中。
语法
public abstract IntBuffer put(int index, int i)
Java
参数: 该方法接受以下参数作为参数。
- index : 将被写入的int值的索引
- i : 要写入的int值
返回值 :该方法返回这个缓冲区。
异常: 该方法会抛出以下异常。
- IndexOutOfBoundsException- 如果index是负数或者不小于缓冲区的极限值。
- ReadOnlyBufferException- 如果这个缓冲区是只读的。
下面的例子说明了put(int index, int i)方法。
例1 :
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer using put() at index 0
ib.put(0, 8);
// putting the value in Intbuffer using put() at index 2
ib.put(2, 9);
// putting the value in Intbuffer using put() at index 1
ib.put(1, 7);
// rewinding the Intbuffer
ib.rewind();
// print the IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Java
输出:
Original IntBuffer: [8, 7, 9]
Java
例2: 演示IndexOutOfBoundsException。
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
// using put() at index 0
ib.put(0, 8);
// putting the value in Intbuffer
// using put() at index 2
ib.put(2, 9);
// putting the value in Intbuffer
// using put() at index -1
System.out.println("Trying to put the value"
+ " at the negative index");
ib.put(-1, 7);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Java
输出:
Trying to put the value at the negative index
Exception throws : java.lang.IndexOutOfBoundsException
Java
例3: 演示ReadOnlyBufferException。
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity using allocate() method
IntBuffer ib = IntBuffer.allocate(capacity);
// Creating a read-only copy of IntBuffer
// using asReadOnlyBuffer() method
IntBuffer ib1 = ib.asReadOnlyBuffer();
System.out.println("Trying to put the Int value"
+ " in read only buffer");
// putting the value in readonly Intbuffer
// using put() method
ib1.put(0, 8);
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Java
输出:
Trying to put the Int value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
Java