📜  用Java实现位矩阵

📅  最后修改于: 2021-09-07 03:00:32             🧑  作者: Mango

BitMatrix 是一个二维数组,其中每个元素都是 0 或 1。我们将实现一个 BitMatrix,以方便基本的位运算,例如 OR、AND、XOR。

方法:

  1. 从Java.util 包导入 BitSet 类。
  2. 使用 BitSet 初始化一个大小为行 x cols 的位数组。默认情况下,集合中的所有位最初都具有值 false。
  3. 现在通过使用 BitSet 类提供的方法,我们可以操作数组。
  4. BitSet 类中的方法有 set、clear、xor、or、and。
  5. 使用 display 方法显示 BitMatrix。
Java
// Java program to demonstrate the
// implementation of BitMatrix
 
import java.util.BitSet;
 
public class BitMatrix {
    public static void main(String[] args)
    {
 
        System.out.println("Bit Matrix Implementation");
 
        try {
            MatrixBuilder bmat = new MatrixBuilder(2, 2);
 
            // All the bitsets in the bitArray will be
            // displayed using display(). The values in
            // bitset refer to the columns which are set
            // to 1.
            bmat.set(0, 0);
            bmat.display();
 
            bmat.set(0, 1);
            bmat.display();
 
            bmat.set(1, 0);
            bmat.display();
 
            bmat.set(1, 1);
            bmat.display();
 
            bmat.clear(0, 1);
            bmat.display();
 
            bmat.and(0, 1);
            bmat.display();
 
            bmat.xor(0, 1);
            bmat.display();
 
            bmat.or(0, 1);
            bmat.display();
        }
        catch (Exception e) {
            System.out.println("Error due to " + e);
        }
    }
}
 
class MatrixBuilder {
    public BitSet[] bitArray;
 
    public MatrixBuilder(int rows, int cols)
    {
        // initializing a bit matrix of size rows x cols.
        bitArray = new BitSet[rows];
 
        int i = 0;
 
        while (i < rows) {
            bitArray[i] = new BitSet(cols);
            i++;
        }
    }
 
    // Method to clear entire array.
    public void clear()
    {
        // Getting the value of number of rows.
        int rows = bitArray.length;
 
        // Getting the value number of columns.
        int cols = bitArray[0].size();
 
        // To clear the bitArray Initialize it once again.
        bitArray = new BitSet[rows];
 
        int i = 0;
        while (i < rows) {
            bitArray[i] = new BitSet(cols);
            i++;
        }
    }
 
    // Method to XOR two rows
    public void xor(int row1, int row2)
    {
        bitArray[row1].xor(bitArray[row2]);
    }
 
    // Here clear() method is overloaded.
    // Method to clear specific bit.
    public void clear(int r, int c)
    {
        bitArray[r].clear(c);
    }
 
    // Method to get a specific bit
    public boolean get(int r, int c)
    {
        return bitArray[r].get(c);
    }
 
    // Method to set a specific bit
    public void set(int r, int c) { bitArray[r].set(c); }
 
    // Method to And two rows
    public void and(int row1, int row2)
    {
        bitArray[row1].and(bitArray[row2]);
    }
 
    // Method to OR two rows
    public void or(int row1, int row2)
    {
        bitArray[row1].or(bitArray[row2]);
    }
 
    // Method to display the bit matrix
    public void display()
    {
        System.out.println("\nBit Matrix : ");
 
        // Here each bitset can be refered as each row
        // we will print all the rows using for loop.
        for (BitSet bs : bitArray)
            System.out.println(bs);
 
        System.out.println();
    }
}


输出
Bit Matrix Implementation

Bit Matrix : 
{0}
{}


Bit Matrix : 
{0, 1}
{}


Bit Matrix : 
{0, 1}
{0}


Bit Matrix : 
{0, 1}
{0, 1}


Bit Matrix : 
{0}
{0, 1}


Bit Matrix : 
{0}
{0, 1}


Bit Matrix : 
{1}
{0, 1}


Bit Matrix : 
{0, 1}
{0, 1}

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live