java.nio.charBuffer類的slice()方法用於創建一個新的char緩衝區,其內容是給定緩衝區內容的共享子序列。
新緩衝區的內容將從該緩衝區的當前位置開始。新緩衝區將顯示緩衝區內容中所做的更改,反之亦然。這兩個緩衝區的位置,限製和標記值將是獨立的。
新緩衝區的位置將為零,其容量和限製將為該緩衝區中剩餘的整數數量,並且其標記將不確定。當且僅當此緩衝區是直接緩衝區時,新緩衝區才是直接緩衝區,並且僅當且僅當此緩衝區是隻讀緩衝區時,它才是隻讀的。
用法:
public abstract CharBuffer slice()
返回值:此方法返回新的char緩衝區。
下麵是說明slice()方法的示例:
示例1:
// Java program to demonstrate
// slice() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 10;
// Creating the CharBuffer
try {
// creating object of Charbuffer
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity);
// putting the value in intbuffer
cb1.put('a');
cb1.put('b');
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(cb1.array()));
// print the CharBuffer position
System.out.println("position: " + cb1.position());
// print the CharBuffer capacity
System.out.println("capacity: " + cb1.capacity());
// Creating a shared subsequance buffer of given CharBuffer
// using slice() method
CharBuffer cb2 = cb1.slice();
// print the shared subsequance buffer
System.out.println("shared subsequance CharBuffer: "
+ Arrays.toString(cb2.array()));
// print the CharBuffer position
System.out.println("position: " + cb2.position());
// print the CharBuffer capacity
System.out.println("capacity: " + cb2.capacity());
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
輸出:
Original CharBuffer: [a, b, , , , , , , , ] position: 2 capacity: 10 shared subsequance CharBuffer: [a, b, , , , , , , , ] position: 0 capacity: 8
示例2:
// Java program to demonstrate
// slice() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 10;
// Creating the CharBuffer
try {
// creating object of charbuffer
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity);
// putting the value in floatbuffer
cb1.put('a');
cb1.put('b');
cb1.put('c');
cb1.put('d');
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(cb1.array()));
// print the CharBuffer position
System.out.println("position: " + cb1.position());
// print the CharBuffer capacity
System.out.println("capacity: " + cb1.capacity());
// Creating a shared subsequance buffer of given CharBuffer
// using slice() method
CharBuffer cb2 = cb1.slice();
cb2.put('k');
cb2.put('l');
// print the shared subsequance buffer
System.out.println("shared subsequance CharBuffer: "
+ Arrays.toString(cb2.array()));
// print the CharBuffer position
System.out.println("position: " + cb2.position());
// print the CharBuffer capacity
System.out.println("capacity: " + cb2.capacity());
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
輸出:
Original CharBuffer: [a, b, c, d, , , , , , ] position: 4 capacity: 10 shared subsequance CharBuffer: [a, b, c, d, k, l, , , , ] position: 2 capacity: 6
相關用法
- Java CharBuffer compact()用法及代碼示例
- Java CharBuffer asReadOnlyBuffer()用法及代碼示例
- Java CharBuffer hasArray()用法及代碼示例
- Java CharBuffer array()用法及代碼示例
- Java CharBuffer arrayOffset()用法及代碼示例
- Java CharBuffer equals()用法及代碼示例
- Java CharBuffer compareTo()用法及代碼示例
- Java CharBuffer duplicate()用法及代碼示例
- Java CharBuffer wrap()用法及代碼示例
- Java CharBuffer allocate()用法及代碼示例
- Java StringReader read(CharBuffer)用法及代碼示例
- Java Reader read(CharBuffer)用法及代碼示例
- Java CharArrayReader read(CharBuffer)用法及代碼示例
- Java CharsetEncoder encode(CharBuffer in)用法及代碼示例
- Java IntBuffer slice()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 CharBuffer slice() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。