Sometimes you can replace the inner loop with System.arraycopy , which will be faster. But I do not believe in memory layout.
If possible, instead of copying the array, consider updating the links. Recall that "multidimensional" arrays in java are arrays of arrays.
In particular,
boolean[] tmp = multi[0]; multi[0] = multi[1]; multi[1] = tmp;
swaps references to two arrays at a value other than zero. This is much faster than copying and then rewriting old values. But sometimes you need a copy (if you do not overwrite the old values), then you cannot do this.
Please note that you should not do this blindly:
multi[0][0] = 1; multi[1] = multi[0]; multi[1][0] = 0; System.err.println(multi[0][0]);
will print 0 because now multi[0] and multi[1] point to the same nested array, and you should have used it.
multi[1] = multi[0].clone();
Note that cloning is also not deep, so multi.clone() will point to the same nested arrays as multi . There is no built-in deep cloning or deep arraycopy in Java, you need to use a loop for this anyway.
But then again, none of them work if you want to copy the second element into the first array of sets. This is the problem of your memory layout.
Recall what your in-memory data structure looks like:
boolean[][][] -> boolean[][] -> boolean[]{ 0, 1 } \ \> boolean[]{ 0, 1 } \> boolean[][] -> boolean[]{ 0, 1 } \> boolean[]{ 0, 1 }
you want to copy one element in each array. They can be everywhere in your memory (each boolean[]... is its own object!), So there is no way to speed it up using primitives - the data is scattered. Perhaps consider changing the layout of the memory , if possible.
Also consider alternatives to boolean arrays. Booleans take 1 byte of memory, but only save one bit (note that this can be faster, so it's not so bad!). But sometimes it makes sense instead to store the entire logical array in BitSet or long , and then work with the actual bit operations. But the gain, sometimes it pays, sometimes it hurts.