Java Tutorial/Collections/BitSet — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:05, 1 июня 2010

Bit Set Operations

public void set(int position)
public void clear(int position)
public boolean get(int position)



{0, 1, 2, 3}


Cloning Bit Sets

The BitSet class implements the Cloneable interface, providing you with a clone() method.



import java.util.BitSet;
public class MainClass {
  public static void main(String args[]) {
    BitSet bites = new BitSet();
    bites.set(0);
    bites.set(1);
    bites.set(2);
    bites.set(3);
    BitSet bitSetClone = (BitSet) bites.clone();
    System.out.println(bites);
    System.out.println(bitSetClone);
  }
}



{0, 1, 2, 3}
{0, 1, 2, 3}


Determining Set Size

A BitSet is a dynamically growing array containing bits.

This dynamically growing structure has two values describing its internal dimensions: a size and a length.



public int size()
public int length()



64
4


Manipulating Sets of Bits

public void and(BitSet set)
public void or(BitSet set)
public void xor(BitSet set)
public void andNot(BitSet set)



{0, 1, 2, 3}
{0, 1, 2, 3}
{0, 1, 2, 3}
{}
{}


Set value to BitSet and then do AND, OR and XOR actions

import java.util.BitSet;
class BitSetDemo {
  public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);
    for (int i = 0; i < 16; i++) {
      if ((i % 2) == 0)
        bits1.set(i);
      if ((i % 5) != 0)
        bits2.set(i);
    }
    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);
    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);
    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);
    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
  }
}





Use BitSet to mark holiday

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());
      }
    }
  }
}