Reputation: 11
boolean []r = new boolean[numberOfStates];
for(int i=0; i<numberOfStates;i++)
r[i]=i==q;
Being q, i and numberOfStates int variables, what does r[i]=i==q; do?
Upvotes: 1
Views: 61
Reputation: 2691
if you initialize array as and q as bellow you will end up with result
true false
public static void main(String[] args) {
int numberOfStates = 2;
int q = 0;
boolean []r = new boolean[numberOfStates];
r[0] = false;
r[1] = false;
for(int i=0; i<numberOfStates;i++)
r[i]=i==q;
for(int i=0; i<numberOfStates;i++)
System.out.println(r[i]);
}
as per my understanding == has higher Precedence than = so r[i]=i==q; will evaluated as i==q (q is 0) it result true ans assigned to r[[0]] to true;
Upvotes: 0
Reputation: 394146
It assigns true to r[i]
if i==q
and false if not.
Which means r[q]
is the only element in the array that would be assigned true.
Since all the values of a boolean array are initialized to false, you can replace this code snippet with :
boolean [] r = new boolean[numberOfStates];
r[q] = true;
Upvotes: 2
Reputation: 24433
i==q
resolves to a boolean, and is assigned as value to r[i]
.
Upvotes: 3