C++程序员学Java系列之二九:数据结构之Bitset

2024-05-25 09:08

本文主要是介绍C++程序员学Java系列之二九:数据结构之Bitset,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

内容参考:https://www.w3cschool.cn/java/java-bitset-class.html

Java Bitset类

一个Bitset类创建一种特殊类型的数组来保存位值。BitSet中数组大小会随需要增加。这和位向量(vector of bits)比较类似。

这是一个传统的类,但它在Java 2中被完全重新设计。

BitSet定义了两个构造方法。

第一个构造方法创建一个默认的对象:

BitSet()

第二个方法允许用户指定初始大小。所有位初始化为0。

BitSet(int size)

BitSet中接口中定义的方法如下表所列:

序号 方法描述
1 void and(BitSet bitSet)
对此目标位 set 和参数位 set 执行逻辑与操作。
2 void andNot(BitSet bitSet)
清除此 BitSet 中所有的位,其相应的位在指定的 BitSet 中已设置。
3 int cardinality( )
返回此 BitSet 中设置为 true 的位数。
4 void clear( )
将此 BitSet 中的所有位设置为 false。
5 void clear(int index)
将索引指定处的位设置为 false。
6 void clear(int startIndex, int endIndex)
将指定的 fromIndex(包括)到指定的 toIndex(不包括)范围内的位设置为 false。
7 Object clone( )
复制此 BitSet,生成一个与之相等的新 BitSet。
8 boolean equals(Object bitSet)
将此对象与指定的对象进行比较。
9 void flip(int index)
将指定索引处的位设置为其当前值的补码。
10 void flip(int startIndex, int endIndex)
将指定的 fromIndex(包括)到指定的 toIndex(不包括)范围内的每个位设置为其当前值的补码。
11 boolean get(int index)
返回指定索引处的位值。
12 BitSet get(int startIndex, int endIndex)
返回一个新的 BitSet,它由此 BitSet 中从 fromIndex(包括)到 toIndex(不包括)范围内的位组成。
13 int hashCode( )
返回此位 set 的哈希码值。
14 boolean intersects(BitSet bitSet)
如果指定的 BitSet 中有设置为 true 的位,并且在此 BitSet 中也将其设置为 true,则返回 ture。
15 boolean isEmpty( )
如果此 BitSet 中没有包含任何设置为 true 的位,则返回 ture。
16 int length( )
返回此 BitSet 的"逻辑大小":BitSet 中最高设置位的索引加 1。
17 int nextClearBit(int startIndex)
返回第一个设置为 false 的位的索引,这发生在指定的起始索引或之后的索引上。
18 int nextSetBit(int startIndex)
返回第一个设置为 true 的位的索引,这发生在指定的起始索引或之后的索引上。
19 void or(BitSet bitSet)
对此位 set 和位 set 参数执行逻辑或操作。
20 void set(int index)
将指定索引处的位设置为 true。
21 void set(int index, boolean v)
 将指定索引处的位设置为指定的值。
22 void set(int startIndex, int endIndex)
将指定的 fromIndex(包括)到指定的 toIndex(不包括)范围内的位设置为 true。
23 void set(int startIndex, int endIndex, boolean v)
将指定的 fromIndex(包括)到指定的 toIndex(不包括)范围内的位设置为指定的值。
24 int size( )
返回此 BitSet 表示位值时实际使用空间的位数。
25 String toString( )
返回此位 set 的字符串表示形式。
26 void xor(BitSet bitSet)
对此位 set 和位 set 参数执行逻辑异或操作。

实例

import java.util.BitSet;public class JavaTest {public static void main(String[] args) {// TODO Auto-generated method stub// 初始化其位的大小为16位,所有初始化的位为0BitSet bits1 = new BitSet(16);// 0000 0000 0000 0000BitSet bits2 = new BitSet(16);// 0000 0000 0000 0000// set some bitsfor (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);// 0101 0101 0101 0101System.out.println("\nInitial pattern in bits2: ");System.out.println(bits2); // 0111 1011 1101 1110// AND bits,对应位都为1才为1bits2.and(bits1);// 0101 0101 0101 0101// 0111 1011 1101 1110//=0101 0001 0101 0100// 14 12   8  6 4  2 System.out.println("\nbits2 AND bits1: ");System.out.println(bits2);// OR bits,对应位有一个1就为1bits2.or(bits1);// 0101 0101 0101 0101// 0101 0001 0101 0100//=0101 0101 0101 0101// 14 12 10 8  6 4  2 0System.out.println("\nbits2 OR bits1: ");System.out.println(bits2);// XOR bits,对应位不同为1,相同为0bits2.xor(bits1);// 0101 0101 0101 0101// 0101 0101 0101 0101// 0000 0000 0000 0000System.out.println("\nbits2 XOR bits1: ");System.out.println(bits2);}
}

运行结果:

Initial pattern in bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}Initial pattern in bits2: 
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}bits2 AND bits1: 
{2, 4, 6, 8, 12, 14}bits2 OR bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}bits2 XOR bits1: 
{}


没什么好说的,概念和C++等其它语言一样;





这篇关于C++程序员学Java系列之二九:数据结构之Bitset的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1001074

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav