An IDEA Live Template for iterating through a BitSet

I love IDEA’s Live Templates, and I often find myself using java.util.BitSet. The BitSet javadoc describes a neat way of iterating through the 1 bits, and I do this often enough that it warranted a custom Live Template. Now all I need to do is hit itbs<TAB> to have this code automagically appear.

The template looks like this:

for(int $INDEX$ = $BS$.nextSetBit(0); $INDEX$ >= 0; $INDEX$ = $BS$.nextSetBit($INDEX$ + 1)) {
  $END$
}

I’ve captured a few screenshots since you also have to setup variables for $INDEX$ and $BS$.

One Comment

  1. Posted September 1, 2005 at 2:33 pm | Permalink

    One minor caveat worth noting: BitSet.nextSetBit() and BitSet.nextClearBit() were introduced with JDK1.4, so if you need your code to run on a pre-1.4 VM, you can’t use this. :-(

Post a Comment

Your email is never shared. Required fields are marked *

*
*