Reputation: 139
public boolean used[] = new boolean[26];
Here is what I have, and it's working great. I know they are all going to be set as false by default. But, as my application is used for a while, some of those are changed to "true". (Which is fine, since that's what my code is supposed to do).
I'm trying to create a "reset" button, which will simulate a reset-like action. Where all variables are restored to what they were when the window was initially created (all drawings go away - just a fresh restart).
And I need all of those true booleans to go back to false in one fell swoop. Any ideas?
Upvotes: 9
Views: 28510
Reputation: 1
boolean a[]= new boolean[nums.length];
Arrays.fill(a, false);
// this will help you fill array of boolean with false.
// remember to -> import java.util.Arrays;
Upvotes: 0
Reputation: 544
You can just recreate your array, it will be initialized by default to false.
That is when you implement the reset yo can do
used[] = new boolean[26];
Upvotes: 2
Reputation:
Arrays.fill uses a rangecheck before filling your array.
public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i=fromIndex; i<toIndex; i++)
a[i] = val;
}
/**
* Check that fromIndex and toIndex are in range, and throw an
* appropriate exception if they aren't.
*/
private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex+")");
if (fromIndex < 0)
throw new ArrayIndexOutOfBoundsException(fromIndex);
if (toIndex > arrayLen)
throw new ArrayIndexOutOfBoundsException(toIndex);
}
if you don't need rangeCheck, you can just use a forloop to fill up your boolean array.
for(int i = 0; i < used.length; ++i){
used[i] = false;
}
Upvotes: 3
Reputation: 81674
Use java.util.Arrays.fill()
:
Arrays.fill(used, false);
Upvotes: 1