java.nio.Buffer类的remaining()方法用于返回当前位置和限制之间的元素数。
用法:
public final int remaining()
返回值:此方法返回此缓冲区中剩余的元素数。
下面是说明remaining()方法的示例:
范例1:
// Java program to demonstrate
// remaining() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declare and initialize the byte array
byte[] bb = { 10, 20, 30 };
// wrap the byte array into ByteBuffer
// using wrap() method
ByteBuffer byteBuffer = ByteBuffer.wrap(bb);
// Typecast ByteBuffer to Buffer
Buffer buffer = (Buffer)byteBuffer;
// get the number of element present in the Buffer
// using remaining() method
int element = buffer.remaining();
// print the buffer
System.out.println("Remaining element in buffer:"
+ element);
}
}
输出:
Remaining element in buffer:3
范例2:
// Java program to demonstrate
// remaining() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating ByteBuffer
// using allocate() method
ByteBuffer byteBuffer = ByteBuffer.allocate(7);
// put byte value in byteBuffer
// using put() method
byteBuffer.put((byte)20); // 1 byte space required
byteBuffer.put((byte)30); // 1 byte space required
byteBuffer.putInt(40); // 4 byte space required
// Typecast ByteBuffer to Buffer
Buffer buffer = (Buffer)byteBuffer;
// get the number of element present in the Buffer
// using remaining() method
int element = buffer.remaining();
// print the buffer
System.out.println("Remaining element in buffer:"
+ element);
}
}
输出:
Remaining element in buffer:1
参考: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#remaining-
相关用法
- Java Buffer position()用法及代码示例
- Java Buffer flip()用法及代码示例
- Java Buffer rewind()用法及代码示例
- Java Buffer mark()用法及代码示例
- Java Buffer reset()用法及代码示例
- Java Buffer hasRemaining()用法及代码示例
- Java Buffer isReadOnly()用法及代码示例
- Java Buffer limit()用法及代码示例
- Java Buffer hasArray()用法及代码示例
- Java Buffer array()用法及代码示例
- Java Buffer isDirect()用法及代码示例
- Java Buffer clear()用法及代码示例
- Java Buffer capacity()用法及代码示例
- Java Buffer arrayOffset()用法及代码示例
- Java Buffer duplicate()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Buffer remaining() methods in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。