日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java的duplicate用法_Java IntBuffer duplicate()用法及代码示例

發布時間:2023/12/18 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java的duplicate用法_Java IntBuffer duplicate()用法及代码示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java.nio.IntBuffer類的duplicate()方法用于創建一個新的IntBuffer,它在所有方面都相同地共享給定緩沖區的內容。

用法:

public abstract IntBuffer duplicate()

返回值:此方法返回新的IntBuffer,它攜帶以前的IntBuffer的內容

下面是說明duplicate()方法的示例:

范例1:使用直接IntBuffer

// Java program to demonstrate duplicate() method

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 ib1 = IntBuffer.allocate(capacity);

// putting the value in Intbuffer

ib1.put(8);

ib1.put(2, 9);

ib1.rewind();

// print the Original IntBuffer

System.out.println("Original IntBuffer:? "

+ Arrays.toString(ib1.array()));

// Creating a duplicate copy of IntBuffer

// using duplicate() method

IntBuffer ib2 = ib1.duplicate();

// print the duplicate copy of IntBuffer

System.out.print("Duplicate IntBuffer: "

+ Arrays.toString(ib2.array()));

}

catch (IllegalArgumentException e) {

System.out.println("IllegalArgumentException catched");

}

catch (ReadOnlyBufferException e) {

System.out.println("ReadOnlyBufferException catched");

}

}

}

輸出:

Original IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]

Duplicate IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]

范例2:使用read-onlyintbuffer

// Java program to demonstrate

// duplicate() method

// using read-onlyIntbuffer

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 ib1 = IntBuffer.allocate(capacity);

// putting the value in Intbuffer

ib1.put(8);

ib1.put(2, 9);

ib1.rewind();

// print the Original IntBuffer

System.out.println("Original IntBuffer:? "

+ Arrays.toString(ib1.array()));

// Creating a read-only copy of IntBuffer

// using asReadOnlyBuffer() method

IntBuffer readonly = ib1.asReadOnlyBuffer();

// print the read-only copy of IntBuffer

System.out.print("read-only IntBuffer:? ");

while (Readonly.hasRemaining())

System.out.print(readonly.get() + ", ");

System.out.println("");

// Rewinding the readonly IntBuffer

readonly.rewind();

// Creating a duplicate copy of IntBuffer

// using duplicate() method

IntBuffer ib2 = readonly.duplicate();

// print the duplicate copy of IntBuffer

System.out.print("Duplicate copy of read-only IntBuffer:? ");

while (ib2.hasRemaining())

System.out.print(ib2.get() + ", ");

System.out.println("");

}

catch (IllegalArgumentException e) {

System.out.println("IllegalArgumentException catched");

}

catch (ReadOnlyBufferException e) {

System.out.println("ReadOnlyBufferException catched");

}

}

}

輸出:

Original IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]

Read-only IntBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,

Duplicate copy of read-only IntBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,

總結

以上是生活随笔為你收集整理的java的duplicate用法_Java IntBuffer duplicate()用法及代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。