java.nio.ShortBuffer類的compact()方法用於壓縮給定的緩衝區。緩衝區當前位置與其極限(如果有)之間的空頭將複製到緩衝區的開頭。也就是說,將索引p = position()的空缺複製到索引零,將索引p + 1的短缺複製到索引一,依此類推,直到索引limit() – 1的短缺複製到索引n = limit() – 1 – p。然後將緩衝區的位置設置為n + 1,並將其限製設置為其容量。如果定義了該標記,則將其丟棄。緩衝區的位置設置為複製的空頭數量,而不是零,以便在調用此方法後可以立即調用另一個相對的put方法。
用法:
public abstract ShortBuffer compact()
返回值:此方法返回具有與此緩衝區相同內容的新ShortBuffer。
異常:如果此緩衝區是隻讀的,則此方法將引發ReadOnlyBufferException。
以下示例程序旨在說明compact()方法:
程序1:
// Java program to demonstrate
// compact() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ShortBuffer
int capacity = 10;
// Creating the ShortBuffer
// creating object of shortbuffer
// and allocating size capacity
ShortBuffer sb = ShortBuffer.allocate(capacity);
// putting the value in shortbuffer
sb.put((short)856);
sb.put((short)961);
sb.put((short)54);
// print the ShortBuffer
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb.array()));
System.out.println("Position: " + sb.position());
System.out.println("limit: " + sb.limit());
// Creating a compacted ShortBuffer of same ShortBuffer
// using compact() method
ShortBuffer shortBuffer = sb.compact();
// print the ShortBuffer
System.out.println("\nCompacted ShortBuffer: "
+ Arrays.toString(shortBuffer.array()));
System.out.println("Position: " + shortBuffer.position());
System.out.println("limit: " + shortBuffer.limit());
// putting the value in compacted shortbuffer
shortBuffer.put((short)961);
// print the ShortBuffer
System.out.println("\nUpdated Compacted ShortBuffer: "
+ Arrays.toString(shortBuffer.array()));
System.out.println("Position: " + shortBuffer.position());
System.out.println("limit: " + shortBuffer.limit());
}
}
輸出:
Original ShortBuffer: [856, 961, 54, 0, 0, 0, 0, 0, 0, 0] Position: 3 limit: 10 Compacted ShortBuffer: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Position: 7 limit: 10 Updated Compacted ShortBuffer: [0, 0, 0, 0, 0, 0, 0, 961, 0, 0] Position: 8 limit: 10
示例2:演示ReadOnlyBufferException。
// Java program to demonstrate
// compact() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ShortBuffer
int capacity = 10;
// Creating the ShortBuffer
try {
// creating object of ShortBuffer
// and allocating size capacity
ShortBuffer sb = ShortBuffer.allocate(capacity);
// putting the value in floatbuffer
sb.put((short)856);
sb.put((short)961);
sb.put((short)6010);
sb.rewind();
// Creating a read-only copy of ShortBuffer
// using asReadOnlyBuffer() method
ShortBuffer sb1 = sb.asReadOnlyBuffer();
// print the ReadOnlyBuffer
System.out.print("ReadOnlyBuffer ShortBuffer: ");
while (sb1.hasRemaining())
System.out.print(sb1.get() + ", ");
System.out.println("");
// print the Position of ShortBuffer sb
System.out.println("\nPosition: " + sb.position());
// print the Limit of ShortBuffer fb
System.out.println("\nlimit: " + sb.limit());
// Creating a compacted ShortBuffer of same ReadOnlyBuffer
// using compact() method
System.out.println("\nTrying to compact the ReadOnlyBuffer sb1");
ShortBuffer floatBuffer = sb1.compact();
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws " + e);
}
}
}
輸出:
ReadOnlyBuffer ShortBuffer: 856, 961, 6010, 0, 0, 0, 0, 0, 0, 0, Position: 0 limit: 10 Trying to compact the ReadOnlyBuffer sb1 Exception throws java.nio.ReadOnlyBufferException
相關用法
- Java DoubleBuffer compact()用法及代碼示例
- Java ByteBuffer compact()用法及代碼示例
- Java FloatBuffer compact()用法及代碼示例
- Java ShortBuffer mark()用法及代碼示例
- Java ShortBuffer hashCode()用法及代碼示例
- Java ShortBuffer put(int, short)用法及代碼示例
- Java ShortBuffer reset()用法及代碼示例
- Java ShortBuffer compareTo用法及代碼示例
- Java ShortBuffer asReadOnlyBuffer()用法及代碼示例
- Java ShortBuffer arrayOffset()用法及代碼示例
- Java ShortBuffer limit()用法及代碼示例
- Java ShortBuffer isDirect()用法及代碼示例
- Java ShortBuffer rewind()用法及代碼示例
- Java ShortBuffer duplicate()用法及代碼示例
- Java ShortBuffer order()用法及代碼示例
注:本文由純淨天空篩選整理自IshwarGupta大神的英文原創作品 ShortBuffer compact() method in Java With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。