What is the result?
A.
1 1 1 1 1
B.
1 2 3 4 5
C.
0 1 2 3 4
D.
0 1 4 3 4
Explanation:
first for-loop
set 0 0 0 0 0
second for-loop increments each
to 1 1 1 1 1
if condition is not given
What is the result?
A.
1 1 1 1 1
B.
1 2 3 4 5
C.
0 1 2 3 4
D.
0 1 4 3 4
Explanation:
first for-loop
set 0 0 0 0 0
second for-loop increments each
to 1 1 1 1 1
if condition is not given
The answer is 00000 new Line 11111 , please make sure that in the first loop the var[i]=new AtomicInteger(); not AtomicInteger(i);
/**
*
* @author Humberto
*/
import java.util.concurrent.atomic.AtomicInteger;
public class Incrementor {
public static void main(String[] args) {
AtomicInteger[] var = new AtomicInteger[5];
for (int i = 0; i < 5; i++) {
var[i] = new AtomicInteger();
}
for (int i = 0; i < var.length; i++) {
var[i].incrementAndGet();
if (i == 2) {
var[i].compareAndSet(1, 4);
}
System.out.print(var[i] + " ");
}
}
}
1 1 4 1 1
A ?
Output:
0 0 0 0 0
1 1 1 1 1