get()
java.nio.LongBuffer類的get()方法用於在給定緩衝區的當前位置讀取Long,然後遞增該位置。
用法:
public abstract Long get()
返回值:此方法在緩衝區的當前位置返回Long值。
拋出:此方法拋出BufferUnderflowException-如果緩衝區的當前位置不小於其限製,則拋出此異常。
下麵是說明get()方法的示例:
範例1:
// Java program to demonstrate
// get() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the LongBuffer
int capacity = 5;
// Creating the LongBuffer
try {
// creating object of Longbuffer
// and allocating size capacity
LongBuffer ib = LongBuffer.allocate(capacity);
// putting the value in Longbuffer
ib.put(8);
ib.put(9);
ib.put(1);
ib.rewind();
// prLong the LongBuffer
System.out.println("Original LongBuffer: "
+ Arrays.toString(ib.array()));
// Reads the Long at this buffer's current position
// using get() method
Long value = ib.get();
// prLong the Long value
System.out.println("Long Value:" + value);
// Reads the Long at this buffer's next position
// using get() method
Long value1 = ib.get();
// prLong the Long value
System.out.print("Next Long Value:" + value1);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Original LongBuffer: [8, 9, 1, 0, 0] Long Value:8 Next Long Value:9
範例2:
// Java program to demonstrate
// get() 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
ib.put(8);
ib.put(9);
// prLong the LongBuffer
System.out.println("Original LongBuffer: "
+ Arrays.toString(ib.array()));
// Reads the Long at this buffer's current position
// using get() method
Long value = ib.get();
// prLong the Long value
System.out.println("Long Value:" + value);
// Reads the Long at this buffer's next position
// using get() method
System.out.print("Since the buffer current position is incremented");
System.out.print(" to greater than its limit ");
Long value1 = ib.get();
// prLong the Long value
System.out.print("Next Long Value:" + value1);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Original LongBuffer: [8, 9, 0] Long Value:0 Since the buffer current position is incremented to greater than its limit Exception throws:java.nio.BufferUnderflowException
get(Long index)
LongBuffer的get(Long index)方法用於讀取指定索引處的文章。
用法:
public abstract Long get(Long index)
參數:此方法將索引(將從中讀取Long的索引)作為參數。
返回值:此方法返回給定索引處的Long值。
異常:此方法引發IndexOutOfBoundsException。如果index為負或不小於緩衝區的限製,則拋出此異常。
下麵是說明get(Long index)方法的示例:
範例1:
// Java program to demonstrate
// get(Long index) 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
ib.put(8);
ib.put(9);
ib.put(6);
// prLong the LongBuffer
System.out.println("Original LongBuffer: "
+ Arrays.toString(ib.array()));
// Reads the Long at the index 0 of the Longbuffer
// using get() method
Long value0 = ib.get(0);
// prLong the Long value
System.out.println("Long Value at index 0:" + value0);
// Reads the Long at the index 1 of the Longbuffer
// using get() method
Long value1 = ib.get(1);
// prLong the Long value
System.out.println("Long Value at index 1:" + value1);
// Reads the Long at the index 2 of the Longbuffer
// using get() method
Long value2 = ib.get(2);
// prLong the Long value
System.out.println("Long Value at index 2:" + value2);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (IndexOutOfBoundsException e) {
System.out.println("ReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Original LongBuffer: [8, 9, 6] Long Value at index 0:8 Long Value at index 1:9 Long Value at index 2:6
範例2:
// Java program to demonstrate
// get() 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
ib.put(6);
ib.put(8);
ib.put(12);
// prLong the LongBuffer
System.out.println("Original LongBuffer: "
+ Arrays.toString(ib.array()));
// Reads the Long at the index 0 of the Longbuffer
// using get() method
Long value0 = ib.get(0);
// prLong the Long value
System.out.println("Long Value at index 0:" + value0);
// Reads the Long at the index 1 of the Longbuffer
// using get() method
Long value1 = ib.get(1);
// prLong the Long value
System.out.println("Long Value at index 1:" + value1);
// Reads the Long at the index 2 of the Longbuffer
// using get() method
System.out.println("Trying to get the Long"
+ " of index greater than its limit ");
Long value2 = ib.get(4);
// prLong the Long value
System.out.println("Long Value at index 2:" + value2);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception thrown:" + e);
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws:" + e);
}
}
}
輸出:
Original LongBuffer: [6, 8, 12] Long Value at index 0:6 Long Value at index 1:8 Trying to get the Long of index greater than its limit Exception thrown:java.lang.IndexOutOfBoundsException
相關用法
- Java LongBuffer put()用法及代碼示例
- 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 get() methods in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。