Java AtomicIntegerArray get()方法及示例

Java AtomicIntegerArray get()方法及示例

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

语法

public final int get(int i)

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

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

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

程序1 :

// 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

程序2

// 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://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#get(int)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程