Java中的位集类方法及其示例|集3

                       BitSet class methods in Set 3.
               /      /      |       |     |             
            and  notand    flip  isEmpty  equal   get   intersect

Java中的位集类方法及示例|集2

null

比特集类方法解释如下:

  1. 和/或 JAVAutil。比特集。和() JAVAutil。比特集。notand() 方法是java。util。位集类方法。 .and()方法使用作为参数传递的位集对位集(目标)执行逻辑and运算。当且仅当正在操作的两个位均为真时,此操作将返回位(set)。 .notand()方法–使用此方法清除所有设置了相应位的位
    Syntax:
    public void and(BitSet bitset)
               or
    public void andNot(BitSet bitste)
    Parameters:
    bitset - set to perform the operation
    
  2. 相等: JAVAutil。比特集。相等() 方法在比较两个位集时发挥作用。它通过比较一个对象和另一个对象来实现。
    Syntax:
    public boolean equals(Object o)
    Parameters: 
    o - the object to compare with
    Overrides: equals in class Object
    Return: if object are same then true; otherwise false
    
  3. get(): JAVAutil。比特集。得到() 方法使用给定位集中的元素创建一个新的位集,这些元素的位置从_Index(包含)到_Index(独占)。
    Syntax:
    public BitSet get(int from_Index,  int to_Index)
    Parameters:
    from_Index - index of the first bit of given BitSet to include
    to_Index - index after the last bit of the BitSet to include
    Throws:
    IndexOutOfBoundsException - if from_Index is negative, or to_Index is negative,
                                or from_Index is larger than to_Index
    

    解释and()、notand()、equal()和get()方法用法的Java代码。

    // Java program explaining BitSet class methods
    // and(), notand(), equal(), get()
    import java.util.*;
    public class GFG
    {
    public static void main(String[] args)
    {
    BitSet bs1 = new BitSet();
    BitSet bs2 = new BitSet();
    // assign values to bs1 using set()
    bs1.set( 7 );
    bs1.set( 1 );
    bs1.set( 2 );
    bs1.set( 4 );
    bs1.set( 3 );
    bs1.set( 6 );
    // assign values to bs2
    bs2.set( 4 );
    bs2.set( 6 );
    bs2.set( 3 );
    bs2.set( 9 );
    bs2.set( 2 );
    // Printing the Bitsets
    System.out.println( "bs1         : " + bs1);
    System.out.println( "bs2         : " + bs2);
    // use of get() to get index 3 to 6 of bs1
    System.out.println( "Use of get() on bs1 : "
    + bs1.get( 1 , 4 ));
    // use of get() to get index 1 to 4 of bs2
    System.out.println( "Use of get() on bs2 : "
    + bs2.get( 1 , 4 ));
    // perform not operation in b/w the sets
    bs1.andNot(bs2);
    System.out.println( "Not b/w bs1 and bs2 : " + bs1);
    // perform and operation between two bitsets
    bs1.and(bs2);
    System.out.println( "And b/w bs1 and bs2 : " + bs1);
    // equal() method to compare the bs1 and bs2
    if (bs1.equals(bs2))
    System.out.println( "Using equal method : Equal" );
    else
    System.out.println( "Using equal method : Not Equal" );
    }
    }

    
    

    输出:

    bs1         : {1, 2, 3, 4, 6, 7}
    bs2         : {2, 3, 4, 6, 9}
    
    Use of get() on bs1 : {0, 1, 2}
    Use of get() on bs2 : {1, 2}
    
    Not b/w bs1 and bs2 : {1, 7}
    And b/w bs1 and bs2 : {}
    
    Using equal method : Not Equal
    
  4. 翻转(): JAVAutil。比特集。翻转(从_索引到_索引) 方法将从给定的from_索引(包含)到指定的to_索引(排除)再到当前值的互补值的每一位设置。
    Syntax:
    public void flip(int from_Index, int to_Index)
    Parameters:
    from_Index - index of the first bit of the given BitSet to flip
    to_Index - index after the last bit of the given BitSet to flip
    Throws:
    IndexOutOfBoundsException - if from_Index is negative, or to_Index is negative, 
                                or from_Index is larger than to_Index
    
  5. 相交(): JAVAutil。比特集。相交() 如果目标位集中的位和给定位集中的位都已设置,则方法返回true。
    Syntax:
    public boolean intersects(BitSet set)
    Parameters:
    set - BitSet to intersect with
    Returns: true if the given BitSet intersects the target BitSet
    
  6. isEmpty(): JAVAutil。比特集。isEmpty() 方法确定给定位集中是否存在设置为true的位。
    Syntax:
    public boolean isEmpty()
    Return: boolean indicating whether the given BitSet is empty
    

    解释intersect()、isEmpty()和flip()方法用法的Java代码。

    // Java program explaining BitSet class methods
    // intersect(), isEmpty(), flip() methods
    import java.util.*;
    public class NewClass
    {
    public static void main(String[] args)
    {
    BitSet bs1 = new BitSet();
    BitSet bs2 = new BitSet();
    // assign values to bs1 using set()
    bs1.set( 7 );
    bs1.set( 1 );
    bs1.set( 2 );
    bs1.set( 4 );
    bs1.set( 3 );
    bs1.set( 6 );
    // assign values to bs2
    bs2.set( 4 );
    bs2.set( 6 );
    bs2.set( 3 );
    bs2.set( 9 );
    bs2.set( 2 );
    // Printing the Bitsets
    System.out.println( "bs1      : " + bs1);
    System.out.println( "bs2      : " + bs2);
    System.out.println( "" );
    // use of intersect() to check if the bitsets
    // intersects
    System.out.println( "Using intersect() : "
    + bs2.intersects(bs1));
    // use of flip() from_index - 1 to_index - 5
    bs1.flip( 1 , 5 );
    System.out.println( "bs1 using flip : " + bs1);
    // use of flip() from_index - 2 to_index - 4
    bs2.flip( 2 , 4 );
    System.out.println( "bs2 using flip : " + bs2);
    System.out.println( "" );
    // use of isEmpty() to check if bitsets are empty
    System.out.println( "Use of isEmpty() : "
    + bs1.isEmpty());
    System.out.println( "Use of isEmpty() : "
    + bs2.isEmpty());
    }
    }

    
    

    输出:

    bs1      : {1, 2, 3, 4, 6, 7}
    bs2      : {2, 3, 4, 6, 9}
    
    Using intersect() : true
    
    bs1 using flip : {6, 7}
    bs2 using flip : {4, 6, 9}
    
    Use of isEmpty() : false
    Use of isEmpty() : false
    

    参考资料: https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html

    本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

    如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享