一,實例代碼
public static void main(String[] args) {
IntBuffer buffer= IntBuffer.allocate(10);
for(int i=0;i<5;i++)
{
int randomNumber=new SecureRandom().nextInt(20);
buffer.put(randomNumber);
}
//讀寫切換
buffer.flip();
while(buffer.hasRemaining())
{
System.out.println(buffer.get());
}
}
}
運行結果:圖片.png
二,源代碼
圖片.png
在buffer的類文件中,翻到第39行及其附近(以下翻譯不是源碼的內容)
/**
* A container for data of a specific primitive type.
*一個存放特定原生數據的容器
* <p> A buffer is a linear, finite sequence of elements of a specific
* primitive type. Aside from its content, the essential properties of a
* buffer are its capacity, limit, and position: </p>
*最重要的除了內容,最基本的三個屬性為capacity,limit,position
* <blockquote>
*
* <p> A buffer's <i>capacity</i> is the number of elements it contains. The
* capacity of a buffer is never negative and never changes. </p>
* capacity是元素的容量,不為負數且從不改變
* <p> A buffer's <i>limit</i> is the index of the first element that should
* not be read or written. A buffer's limit is never negative and is never
* greater than its capacity. </p>
* 第一個不能讀的下標,不能為空,且不能大于容量(capacity)
* <p> A buffer's <i>position</i> is the index of the next element to be
* read or written. A buffer's position is never negative and is never
* greater than its limit. </p>
* position指向下一個讀或者寫的位置,不能大于limit
* </blockquote>
三,圖解
圖片.png
圖片.png
圖片.png