13

Is there a method in Java for setting all values in a boolean array to true?

Obviously I could do this with a for loop, but if I have (for example) a large 3D array, I imagine using a loop would be quite inefficient.

Is there any method in Java to set all values in a certain array to true, or alternatively to set all values to true when the array is initialised?

(e.g

boolean[][][] newBool = new boolean[100][100][100];
newBool.setAllTrue();

//Rather than

for(int a = 0; a < 100; a++) {
    for(int b = 0; b < 100; b++) {
        for(int c = 0; c < 100; c++) {
            newBool[a][b][c] = true;
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Fraser Price
  • 899
  • 6
  • 15
  • 36
  • 2
    @AndrewThompson he has good reasons to "imagine", as in other more powerful languages like C and C++ you can do it with memset, which is far more efficient than a loop. However, in Java this is not possible. – HAL9000 Jan 01 '14 at 15:57
  • possible duplicate of [Fastest way to set all values of an array?](http://stackoverflow.com/questions/9128737/fastest-way-to-set-all-values-of-an-array) – Roger Rowland Jan 01 '14 at 15:58
  • If you have an array of booleans, why not replace it with an int and perform a bitwise operation? – Amir Afghani Jan 01 '14 at 15:59
  • [Here](http://stackoverflow.com/a/7118208/1043824) is one solution to do so *without using a loop*. :D – inquisitive Jan 01 '14 at 16:10
  • @AndrewThompson I didn't say I imagined there would be another way. I just came to the conclusion that running through 100^3 values would be quite inefficient... – Fraser Price Jan 01 '14 at 16:13

4 Answers4

17

You could use Java 7's Arrays.fill which assigns a specified value to every element of the specified array...so something like. This is still using a loop but at least is shorter to write.

boolean[] toFill = new boolean[100] {};
Arrays.fill(toFill, true);
Jack
  • 370
  • 2
  • 4
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/Arrays.java#Arrays.fill%28java.lang.Object%5B%5D%2Cjava.lang.Object%29 – Amir Afghani Jan 01 '14 at 16:03
  • 1
    -1 OP Specifically asked for 3D arrays. `Arrays.fill` work for 1D only. – inquisitive Jan 01 '14 at 16:05
  • not so, you can do the same thing for a 3D array, I just assumed he could extrapolate himself boolean[][][] toFill = new boolean[100][100][100] {}; – Jack Jan 01 '14 at 16:06
  • If Arrays.fill uses a loop internally how is it any better efficiency wise than OP's example of using a loop? – takendarkk Jan 01 '14 at 16:08
  • yes you're right, it's the same as a loop. just more concise. I don't think there's a way in Java to do it without a loop. – Jack Jan 01 '14 at 16:14
2

There is no shortcut in this situation. and the best option is using a for loop. there might be several other options, like setting the value while declaring (!!). Or you can use Arrays.fill methods but internally it will use loop. or if possible then toggle the use of your values.

stinepike
  • 54,068
  • 14
  • 92
  • 112
1

It's much better to use java.util.BitSet instead of array of booleans. In a BitSet you can set values in some range. It's memory effective because it uses array of longs for internal state.

BitSet bSet = new BitSet(10);
bSet.set(0,10);
Kirill Solokhov
  • 400
  • 4
  • 16
0
boolean[] isPrime = new boolean[10];
 
Arrays.fill(isPrime, true);

This will assign all the elements of the array with true. Because by default the boolean array has all elements as false.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33