I'm trying to check if a particular boolean variable has been set to true or false explicitly by my code.
By default, a boolean array is initialized to false. So I'm unable to use it to verify my previous state as part of a DP problem. I thought of having a set containing the indices of all the modified elements. But I wanted to know if there was a more elegant way of doing this. Another way could be to stop default initialization though I can't seem to find how to do that.
public static boolean testMethod(int N) {
boolean b[] = new boolean[N+1];
return canWin(N,b);
}
public static boolean SubMethod(int N, boolean[] b){
if(b[N] != null)// This is where I want to check(null check does not work)
return b[N];
for(int i = 1; i<= N/2; i++){
if(N%i == 0){
if(!SubMethod(N-i, b)){
b[N] = true;
return true;
}
}
}
b[N] = false;
return false;
}
boolean
is a primitive type. Primitive types in java are never null. By default boolean is set to false
.
If you want to leverage null, use Boolean class
eg:
Boolean b[] = new Boolean[N+1];
Then,
if(b[N] != null)//
will work for you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.