put(Long i)
java.nio.LongBuffer類的put(Long i)方法用於將給定的Long值寫入當前位置的新創建的Long緩衝區中,然後遞增該位置。
用法:
public abstract LongBuffer put(Long i)
參數:此方法將Long值i作為要寫入Long緩衝區的參數。
返回值:此方法返回此緩衝區,在其中插入Long值。
異常:此方法引發以下異常:
- BufferOverflowException-如果此緩衝區的當前位置不小於其限製
- ReadOnlyBufferException-如果此緩衝區是隻讀的
下麵是說明put(Long i)方法的示例:
範例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 LongBuffer
int capacity = 3;
// Creating the LongBuffer
try {
// creating object of Longbuffer
// and allocating size capacity
LongBuffer ib = LongBuffer.allocate(capacity);
// putting the value in Longbuffer using put() method
ib.put(8);
ib.put(9);
ib.put(7);
ib.rewind();
// prLong the LongBuffer
System.out.println("Original LongBuffer: "
+ Arrays.toString(ib.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws:" + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Original LongBuffer: [8, 9, 7]
範例2:演示BufferOverflowException。
// Java program to demonstrate
// put() method
// 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 LongBuffer
int capacity = 3;
// Creating the LongBuffer
try {
// creating object of Longbuffer
// and allocating size capacity
LongBuffer ib = LongBuffer.allocate(capacity);
// putting the value in Longbuffer using put() method
ib.put(8);
ib.put(9);
ib.put(7);
System.out.println("Trying to put the Long at the "
+ "position more than its limit");
ib.put(7);
// rewind the Longbuffer
ib.rewind();
// prLong the LongBuffer
System.out.println("Original LongBuffer: "
+ Arrays.toString(ib.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws:" + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Trying to put the Long at the position more than its limit Exception throws:java.nio.BufferOverflowException
範例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 LongBuffer
int capacity = 3;
// Creating the LongBuffer
try {
// creating object of Longbuffer
// and allocating size capacity using allocate() method
LongBuffer ib = LongBuffer.allocate(capacity);
// Creating a read-only copy of LongBuffer
// using asReadOnlyBuffer() method
LongBuffer ib1 = ib.asReadOnlyBuffer();
System.out.println("Trying to put the Long value"
+ " in read only buffer");
// putting the value in readonly Longbuffer
// using put() method
ib1.put(8);
}
catch (BufferOverflowException e) {
System.out.println("Exception throws:" + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Trying to put the Long value in read only buffer Exception throws:java.nio.ReadOnlyBufferException
put(Long index, Long i)
java.nio.LongBuffer類的put(Long index,Long i)方法用於將給定Long值寫入給定索引處的緩衝區。
用法:
public abstract LongBuffer put(Long index, Long i)
參數:此方法將以下參數作為參數:
- index:將要寫的Long的索引
- i:要寫入的Long值
返回值:此方法返回此緩衝區。
異常:此方法引發以下異常:
- IndexOutOfBoundsException-如果索引為負或不小於緩衝區的限製
- ReadOnlyBufferException-如果此緩衝區是隻讀的
以下示例說明了put(Long index,Long i)方法:
範例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 LongBuffer
int capacity = 3;
// Creating the LongBuffer
try {
// creating object of Longbuffer
// and allocating size capacity
LongBuffer ib = LongBuffer.allocate(capacity);
// putting the value in Longbuffer using put() at index 0
ib.put(0, 8);
// putting the value in Longbuffer using put() at index 2
ib.put(2, 9);
// putting the value in Longbuffer using put() at index 1
ib.put(1, 7);
// rewinding the Longbuffer
ib.rewind();
// prLong the LongBuffer
System.out.println("Original LongBuffer: "
+ Arrays.toString(ib.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws:" + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Original LongBuffer: [8, 7, 9]
範例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 LongBuffer
int capacity = 3;
// Creating the LongBuffer
try {
// creating object of Longbuffer
// and allocating size capacity
LongBuffer ib = LongBuffer.allocate(capacity);
// putting the value in Longbuffer
// using put() at index 0
ib.put(0, 8);
// putting the value in Longbuffer
// using put() at index 2
ib.put(2, 9);
// putting the value in Longbuffer
// using put() at index -1
System.out.println("Trying to put the value"
+ " at the negative index");
ib.put(-1, 7);
}
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 LongBuffer
int capacity = 3;
// Creating the LongBuffer
try {
// creating object of Longbuffer
// and allocating size capacity using allocate() method
LongBuffer ib = LongBuffer.allocate(capacity);
// Creating a read-only copy of LongBuffer
// using asReadOnlyBuffer() method
LongBuffer ib1 = ib.asReadOnlyBuffer();
System.out.println("Trying to put the Long value"
+ " in read only buffer");
// putting the value in readonly Longbuffer
// using put() method
ib1.put(0, 8);
}
catch (BufferOverflowException e) {
System.out.println("Exception throws:" + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Trying to put the Long value in read only buffer Exception throws:java.nio.ReadOnlyBufferException
相關用法
- Java LongBuffer get()用法及代碼示例
- Java LongBuffer hasArray()用法及代碼示例
- Java LongBuffer compact()用法及代碼示例
- Java LongBuffer equals()用法及代碼示例
- Java LongBuffer asReadOnlyBuffer()用法及代碼示例
- Java LongBuffer duplicate()用法及代碼示例
- Java LongBuffer slice()用法及代碼示例
- Java LongBuffer allocate()用法及代碼示例
- Java LongBuffer wrap()用法及代碼示例
- Java LongBuffer arrayOffset()用法及代碼示例
- Java LongBuffer reset()用法及代碼示例
- Java LongBuffer limit()用法及代碼示例
- Java LongBuffer clear()用法及代碼示例
- Java LongBuffer mark()用法及代碼示例
- Java LongBuffer flip()用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 LongBuffer put() methods in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。