📌  相关文章
📜  Java中的 AtomicIntegerArray get() 方法及示例

📅  最后修改于: 2022-05-13 01:55:45.903000             🧑  作者: Mango

Java中的 AtomicIntegerArray get() 方法及示例

Java.util.concurrent.atomic.AtomicIntegerArray.get()是Java中的一个内置方法,它获取 AtomicIntegerArray 任意位置的当前值。该方法以索引值作为参数,返回该索引处的值。

句法:

public final int get(int i)

参数:该函数接受单个参数,要获取的索引值。

返回值:函数返回索引i处的当前值。

下面的程序说明了上述方法:

方案一:

// Java program that demonstrates
// the get() function
  
import java.util.concurrent.atomic.AtomicIntegerArray;
  
public class GFG {
    public static void main(String args[])
    {
        // Initializing an array
        int a[] = { 1, 2, 3, 4, 5 };
  
        // Initializing an AtomicIntegerArray with array a
        AtomicIntegerArray arr = new AtomicIntegerArray(a);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array : " + arr);
  
        // Index to get
        int idx = 2;
  
        // Using get() to retrieve value at idx
        int val = arr.get(idx);
  
        // Displaying the value at idx
        System.out.println("Value at index " + idx
                           + " is " + val);
    }
}
输出:
The array : [1, 2, 3, 4, 5]
Value at index 2 is 3

方案二:

// Java program that demonstrates
// the get() function
  
import java.util.concurrent.atomic.AtomicIntegerArray;
  
public class GFG {
    public static void main(String args[])
    {
        // Initializing an array
        int a[] = { 12, 22, 23, 24, 25 };
  
        // Initializing an AtomicIntegerArray with array a
        AtomicIntegerArray arr = new AtomicIntegerArray(a);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array : " + arr);
  
        // Index to get
        int idx = 4;
  
        // Using get() to retrieve value at idx
        int val = arr.get(idx);
  
        // Displaying the value at idx
        System.out.println("Value at index " + idx
                           + " is " + val);
    }
}
输出:
The array : [12, 22, 23, 24, 25]
Value at index 4 is 25

参考: https: Java/util/concurrent/atomic/AtomicIntegerArray.html#get(int)