get()
java.nio.IntBuffer類的get()方法用於讀取給定緩衝區當前位置的int,然後遞增該位置。
用法:
public abstract int get()
返回值:此方法返回緩衝區當前位置的int值。
拋出:此方法拋出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 IntBuffer
int capacity = 5;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
ib.put(8);
ib.put(9);
ib.put(1);
ib.rewind();
// print the IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib.array()));
// Reads the Int at this buffer's current position
// using get() method
int value = ib.get();
// print the Int value
System.out.println("Int Value:" + value);
// Reads the Int at this buffer's next position
// using get() method
int value1 = ib.get();
// print the Int value
System.out.print("Next Int 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 IntBuffer: [8, 9, 1, 0, 0] Int Value:8 Next Int Value:9
範例2:演示java.nio.BufferUnderflowException
// 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 IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
ib.put(8);
ib.put(9);
// print the IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib.array()));
// Reads the Int at this buffer's current position
// using get() method
int value = ib.get();
// print the Int value
System.out.println("Int Value:" + value);
// Reads the Int 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 ");
int value1 = ib.get();
// print the Int value
System.out.print("Next Int 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 IntBuffer: [8, 9, 0] Int Value:0 Since the buffer current position is incremented to greater than its limit Exception throws:java.nio.BufferUnderflowException
get(int index)
IntBuffer的get(int index)方法用於讀取指定索引處的文章。
用法:
public abstract int get(int index)
參數:此方法將索引(將從中讀取int的索引)作為參數。
返回值:此方法返回給定索引處的int值。
異常:此方法引發IndexOutOfBoundsException。如果index為負或不小於緩衝區的限製,則拋出此異常。
以下示例說明了get(int index)方法:
範例1:
// Java program to demonstrate
// get(int index) method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
ib.put(8);
ib.put(9);
ib.put(6);
// print the IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib.array()));
// Reads the Int at the index 0 of the Intbuffer
// using get() method
int value0 = ib.get(0);
// print the Int value
System.out.println("Int Value at index 0:" + value0);
// Reads the Int at the index 1 of the Intbuffer
// using get() method
int value1 = ib.get(1);
// print the Int value
System.out.println("Int Value at index 1:" + value1);
// Reads the Int at the index 2 of the Intbuffer
// using get() method
int value2 = ib.get(2);
// print the Int value
System.out.println("Int 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 IntBuffer: [8, 9, 6] Int Value at index 0:8 Int Value at index 1:9 Int Value at index 2:6
範例2:演示IndexOutOfBoundsException
// 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 IntBuffer
int capacity = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
ib.put(6);
ib.put(8);
ib.put(12);
// print the IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib.array()));
// Reads the Int at the index 0 of the Intbuffer
// using get() method
int value0 = ib.get(0);
// print the Int value
System.out.println("Int Value at index 0:" + value0);
// Reads the Int at the index 1 of the Intbuffer
// using get() method
int value1 = ib.get(1);
// print the Int value
System.out.println("Int Value at index 1:" + value1);
// Reads the Int at the index 2 of the Intbuffer
// using get() method
System.out.println("Trying to get the Int"
+ " of index greater than its limit ");
int value2 = ib.get(4);
// print the Int value
System.out.println("Int 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 IntBuffer: [6, 8, 12] Int Value at index 0:6 Int Value at index 1:8 Trying to get the Int of index greater than its limit Exception thrown:java.lang.IndexOutOfBoundsException
相關用法
- Java IntBuffer put()方法用法及代碼示例
- Java IntBuffer reset()用法及代碼示例
- Java IntBuffer arrayOffset()用法及代碼示例
- Java IntBuffer asReadOnlyBuffer()用法及代碼示例
- Java IntBuffer allocate()用法及代碼示例
- Java IntBuffer equals()用法及代碼示例
- Java IntBuffer compareTo()用法及代碼示例
- Java IntBuffer slice()用法及代碼示例
- Java IntBuffer compact()用法及代碼示例
- Java IntBuffer hasArray()用法及代碼示例
- Java IntBuffer array()用法及代碼示例
- Java IntBuffer wrap()用法及代碼示例
- Java IntBuffer flip()用法及代碼示例
- Java IntBuffer duplicate()用法及代碼示例
- Java IntBuffer mark()用法及代碼示例
注:本文由純淨天空篩選整理自nitin_sharma大神的英文原創作品 IntBuffer get() methods in Java | Set 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。