當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BitSet or()用法及代碼示例


先決條件: Java BitSet |套裝1

BitSet是java.util包中定義的類。它創建一個由布爾值表示的位數組。它執行調用方BitSet與被調用BitSet的邏輯或。僅當它為true且BitSet參數的值為true時,才會設置此BitSet。

用法:


public void and(BitSet set);

例子:

Input:
set1:{1, 2, 4}
set2:{2, 3, 4}

Output: After performing set1.or(set2)
set2:{1, 2, 3, 4}

程序:

// Java program illustrating Bitset Class or() function. 
import java.util.*; 
public class GFG { 
    public static void main(String[] args) 
    { 
        // Constructors of BitSet class 
        BitSet set1 = new BitSet(); 
        BitSet set2 = new BitSet(6); 
  
        /* set is BitSet class method 
        expalined in next articles */
        set1.set(21); 
        set1.set(45); 
        set1.set(8); 
        set1.set(23); 
  
        // assign values to set2 
        set2.set(12); 
        set2.set(89); 
        set2.set(21); 
        set2.set(78); 
        set2.set(93); 
        set2.set(3); 
  
        // Printing the 2 Bitsets 
        System.out.println("set1:" + set1); 
        System.out.println("set2:" + set2); 
  
        // Performing logical AND 
        // on set2 set with set1 
        set2.or(set1); 
  
        // set2 set after Performing AND 
        System.out.println("After Performing OR:"); 
        System.out.println(set2); 
    } 
}
輸出:
set1:{8, 21, 23, 45}
set2:{3, 12, 21, 78, 89, 93}
After Performing OR:
{3, 8, 12, 21, 23, 45, 78, 89, 93}

相關文章:

  1. 位集類示例集1
  2. 位集類示例集2
  3. 位集類示例集3


相關用法


注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Java BitSet | or()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。