zoukankan      html  css  js  c++  java
  • 位集合(BitSet)| Java数据结构

    Java Bitset类

    BitSet使用示例:

    import java.util.BitSet;
    
    public class BitSetDemo {
    
        public static void main(String args[]) {
            BitSet bits1 = new BitSet(16);
            BitSet bits2 = new BitSet(16);
    
            // set some bits
            for (int i = 0; i < 16; i++) {
                if ((i % 2) == 0) {
                    bits1.set(i);
                }
                if ((i % 5) != 0) {
                    bits2.set(i);
                }
            }
            System.out.println(bits1);
            System.out.println(bits2);
            System.out.println("--------------------");
            
            //void and(BitSet set)
            /*bits2.and(bits1);
            System.out.println(bits1);
            System.out.println(bits2);*/
            
            //void andNot(BitSet set)
            /*bits2.andNot(bits1);
            System.out.println(bits1);
            System.out.println(bits2);*/
            
            //int cardinality( )
            /*System.out.println(bits1.cardinality());
            System.out.println(bits2.cardinality());*/
            
            //void clear( )
    
            /*bits1.clear();
            System.out.println(bits1);
            System.out.println(bits2);*/
            
            // void clear(int index)
            /*bits1.clear(2);
            System.out.println(bits1);
            System.out.println(bits2);*/
    
            // void clear(int startIndex, int endIndex)
            /*bits1.clear(2,6);//前开后闭
            System.out.println(bits1);
            System.out.println(bits2);*/
            
            //    Object clone( )
            //boolean equals(Object bitSet)
    //        BitSet bits3 = (BitSet) bits1.clone();
    //        System.out.println(bits3);
    //        System.out.println(bits3.equals(bits1));
            
            //void flip(int index)
            /*bits1.flip(0);
            bits1.flip(2);
            bits1.flip(3);
            System.out.println(bits1);
            System.out.println(bits2);*/
            
            //boolean get(int index)
    //        System.out.println(bits1.get(4));
    //        System.out.println(bits1.get(5));
            
            //BitSet get(int startIndex, int endIndex)
    //        BitSet bits3=bits1.get(2,6);
    //        System.out.println(bits3);
            
            //int hashCode( )
    //        System.out.println(bits1.hashCode());
    //        System.out.println(bits1.hashCode());
            
            //boolean intersects(BitSet bitSet)
            /*BitSet bits3 = new BitSet(16);
            System.out.println(bits3);
            bits3.set(0);
            System.out.println(bits3.intersects(bits1));
            System.out.println(bits3.intersects(bits2));
            System.out.println(bits1);
            System.out.println(bits2);
            System.out.println(bits3);*/
            
            //boolean isEmpty( )    
    //        bits1.clear();
    //        System.out.println(bits1);
    //        System.out.println(bits1.isEmpty());
    
            //...
            
            //OR bits
    //         bits2.or(bits1);
    //         System.out.println("bits2 OR bits1: ");
    //         System.out.println(bits2);
            
            //
    //        System.out.println(bits1.size());
            
    //        System.out.println(bits1.toString());
            
            bits1.xor(bits2);
            System.out.println(bits1);
        }
    }
  • 相关阅读:
    Hello iOS
    钝化程序、逻辑冻结、冻结程序的延续、瞬间转移
    对字符串操作的各种笔试题
    .NET各大平台数据列表控件绑定原理及比较(WebForm、Winform、WPF)
    多线程计算----pthread
    Xcode的Hello World(简单易懂)
    two sets of Qt binaries into the same process的解决办法
    微软新一代输入法框架 TSF
    Starting the application on Mac does not work(拷贝platforms到不同的位置,才能解决问题),还可设置DYLD_PRINT_LIBRARIES=1 观察动态库
    dddd
  • 原文地址:https://www.cnblogs.com/whoknows1/p/9995645.html
Copyright © 2011-2022 走看看