Use BitSet to mark holiday : BitSet « Collections « Java Tutorial






import java.util.BitSet;

public class HolidaySked {
  BitSet sked;

  public HolidaySked() {
    sked = new BitSet(365);
    int[] holiday = { 1, 15, 50, 148, 185, 246, 281, 316, 326, 359 };
    for (int i = 0; i < holiday.length; i++) {
      addHoliday(holiday[i]);
    }
  }

  public void addHoliday(int dayToAdd) {
    sked.set(dayToAdd);
  }

  public boolean isHoliday(int dayToCheck) {
    boolean result = sked.get(dayToCheck);
    return result;
  }

  public static void main(String[] arguments) {
    HolidaySked cal = new HolidaySked();
    if (arguments.length > 0) {
      try {
        int whichDay = Integer.parseInt(arguments[0]);
        if (cal.isHoliday(whichDay)) {
          System.out.println("Day number " + whichDay + " is a holiday.");
        } else {
          System.out.println("Day number " + whichDay + " is not a holiday.");
        }
      } catch (NumberFormatException nfe) {
        System.out.println("Error: " + nfe.getMessage());
      }
    }
  }
}








9.49.BitSet
9.49.1.Bit Set Operations
9.49.2.Manipulating Sets of Bits
9.49.3.Determining Set Size
9.49.4.Cloning Bit Sets
9.49.5.Set value to BitSet and then do AND, OR and XOR actions
9.49.6.Use BitSet to mark holiday