Java BitSet toString()方法及示例
java.util.BitSet.toString()是BitSet类中的一个内置方法,用于获得一组由”, “分隔的条目形式的比特的字符串表示。 所以基本上toString()方法是用来将BitSet的所有元素转换成字符串。除此之外,对于集合状态下的每一个比特的索引,该索引的十进制表示也包含在结果中。
语法
Bit_Set.toString()
参数: 该方法不接受任何参数。
返回值: 该方法返回由给定BitSet元素的字符串表示组成的集合。
下面的程序说明了java.util.BitSet.toString()方法的工作:
程序1 :
// Java code to illustrate toString()import java.util.*; public class BitSet_Demo { public static void main(String args[]) { // Creating an empty BitSet BitSet init_bitset = new BitSet(); // Use set() method to add elements into the Set init_bitset.set(40); init_bitset.set(25); init_bitset.set(31); init_bitset.set(100); init_bitset.set(53); // Displaying the BitSet System.out.println("BitSet: " + init_bitset); // Displaying the toString() working System.out.println("The String representation: " + init_bitset.toString()); }}输出:
BitSet: {25, 31, 40, 53, 100}The String representation: {25, 31, 40, 53, 100}程序2
// Java code to illustrate toString()import java.util.*; public class BitSet_Demo { public static void main(String args[]) { // Creating an empty BitSet BitSet init_bitset = new BitSet(); // Use set() method to add elements into the Set init_bitset.set(10); init_bitset.set(20); init_bitset.set(30); init_bitset.set(40); init_bitset.set(50); // Displaying the BitSet System.out.println("BitSet: " + init_bitset); // Displaying the toString() working System.out.println("The String representation: " + init_bitset.toString()); }}输出:
BitSet: {10, 20, 30, 40, 50}The String representation: {10, 20, 30, 40, 50} 