Java.util.concurrent.atomic.AtomicIntegerArray.getAndDecrement()是Java中的一種內置方法,可將給定索引的值原子遞減1。此方法獲取AtomicIntegerArray的索引值,並返回該索引處的值,然後遞減該索引處的值。函數getAndDecrement()與decrementAndGet()類似,但後一個函數在減量後返回值,而前一個函數在減量前返回值。
用法:
public final int getAndDecrement(int i)
參數:該函數接受單個參數i,該參數是執行一個操作減1的索引。
返回值:該函數在以int為索引的減量運算之前返回值。
以下示例程序旨在說明上述方法:
示例1:
// Java program that demonstrates
// the getAndDecrement() 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 where operation is performed
int idx = 3;
// Decrementing the value at
// idx applying getAndDecrement
// and storing previous value
int prev = arr.getAndDecrement(idx);
// The previous value at idx
System.out.println("Value at index " + idx
+ " before decrement is "
+ prev);
// Displaying the AtomicIntegerArray
System.out.println("The array after decrement : "
+ arr);
}
}
輸出:
The array : [1, 2, 3, 4, 5] Value at index 3 before decrement is 4 The array after decrement : [1, 2, 3, 3, 5]
示例2:
// Java program that demonstrates
// the getAndDecrement() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
int a[] = { 10, 20, 30, 40, 50 };
// Initializing an AtomicIntegerArray with array a
AtomicIntegerArray arr = new AtomicIntegerArray(a);
// Displaying the AtomicIntegerArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 0;
// Decrementing the value at
// idx applying getAndDecrement
// and storing previous value
int prev = arr.getAndDecrement(idx);
// The previous value at idx
System.out.println("Value at index " + idx
+ " before decrement is "
+ prev);
// Displaying the AtomicIntegerArray
System.out.println("The array after decrement : "
+ arr);
}
}
輸出:
The array : [10, 20, 30, 40, 50] Value at index 0 before decrement is 10 The array after decrement : [9, 20, 30, 40, 50]
相關用法
- Java AtomicLongArray getAndDecrement()用法及代碼示例
- Java AtomicInteger getAndDecrement()用法及代碼示例
- Java AtomicLong getAndDecrement()用法及代碼示例
- Java AtomicIntegerArray set()用法及代碼示例
- Java AtomicIntegerArray get()用法及代碼示例
- Java AtomicIntegerArray toString()用法及代碼示例
- Java AtomicIntegerArray addAndGet()用法及代碼示例
- Java AtomicIntegerArray getAndSet()用法及代碼示例
- Java AtomicIntegerArray getAndAdd()用法及代碼示例
- Java AtomicIntegerArray incrementAndGet()用法及代碼示例
- Java AtomicIntegerArray length()用法及代碼示例
- Java AtomicIntegerArray getAndIncrement()用法及代碼示例
- Java AtomicIntegerArray compareAndSet()用法及代碼示例
- Java AtomicIntegerArray getAndUpdate()用法及代碼示例
- Java AtomicIntegerArray accumulateAndGet()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 AtomicIntegerArray getAndDecrement() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。