java.nio.ShortBuffer類的put()方法用於將給定的short寫入給定索引處的該緩衝區。
用法:
public abstract ShortBuffer put(int index, short s)
參數:
- index:此參數指定寫入短路的索引。它是可選的。
- s:此參數指定要寫入的短值
返回值:此方法返回此緩衝區。
異常:
- IndexOutOfBoundsException-如果index為負或不小於緩衝區的限製。
- ReadOnlyBufferException-如果此緩衝區是隻讀的
以下示例程序旨在說明put()方法:
程序1::
// Java program to demonstrate put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ShortBuffer
int capacity = 3;
// Creating the ShortBuffer
try {
// creating object of shortbuffer
// and allocating size capacity
ShortBuffer sb
= ShortBuffer.allocate(capacity);
// putting the value in shortbuffer
// using put() at index 0
sb.put(0, (short)400);
// putting the value in shortbuffer
// using put() at index 2
sb.put(2, (short)1000);
// putting the value in shortbuffer
// using put() at index 1
sb.put(1, (short)30);
// rewinding the shortbuffer
sb.rewind();
// print the ShortBuffer
System.out.println("Original ShortBuffer:"
+ Arrays.toString(sb.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws:" + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Original ShortBuffer:[400, 30, 1000]
程序2::
演示IndexOutOfBoundsException。
// Java program to demonstrate put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ShortBuffer
int capacity = 3;
// Creating the ShortBuffer
try {
// creating object of shortbuffer
// and allocating size capacity
ShortBuffer sb
= ShortBuffer.allocate(capacity);
// putting the value in shortbuffer
// using put() at index 0
sb.put(0, (short)31);
// putting the value in shortbuffer
// using put() at index 2
sb.put(2, (short)49);
// putting the value in shortbuffer
// using put() at index -1
System.out.println("Trying to put the value"
+ " at the negative index");
sb.put(-1, (short)27);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws:" + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Trying to put the value at the negative index Exception throws:java.lang.IndexOutOfBoundsException
程序3::演示ReadOnlyBufferException。
// Java program to demonstrate put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ShortBuffer
int capacity = 3;
// Creating the ShortBuffer
try {
// creating object of shortbuffer
// and allocating size capacity
// using allocate() method
ShortBuffer sb
= ShortBuffer.allocate(capacity);
// Creating a read-only copy of ShortBuffer
// using asReadOnlyBuffer() method
ShortBuffer sb1 = sb.asReadOnlyBuffer();
System.out.println("Trying to put the float value"
+ " in read only buffer");
// putting the value in readonly shortbuffer
// using put() method
sb1.put(0, (short)13);
}
catch (BufferOverflowException e) {
System.out.println("Exception throws:" + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Trying to put the float value in read only buffer Exception throws:java.nio.ReadOnlyBufferException
參考: https://docs.oracle.com/javase/9/docs/api/java/nio/ShortBuffer.html#put-int-short-
相關用法
- Java ByteBuffer putInt()用法及代碼示例
- Java Shorts.indexOf(short[] array, short target)用法及代碼示例
- Java Shorts.indexOf(short[] array, short[] target)用法及代碼示例
- Java ShortBuffer duplicate()用法及代碼示例
- Java ShortBuffer compact()用法及代碼示例
- Java ShortBuffer asReadOnlyBuffer()用法及代碼示例
- Java ShortBuffer hasArray()用法及代碼示例
- Java ShortBuffer equals()用法及代碼示例
- Java ShortBuffer toString()用法及代碼示例
- Java ShortBuffer compareTo用法及代碼示例
- Java ShortBuffer arrayOffset()用法及代碼示例
- Java ShortBuffer allocate()用法及代碼示例
- Java ShortBuffer slice()用法及代碼示例
- Java ShortBuffer order()用法及代碼示例
- Java ShortBuffer reset()用法及代碼示例
注:本文由純淨天空篩選整理自IshwarGupta大神的英文原創作品 ShortBuffer put(int, short) method in Java With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。