flip()方法有兩種變體。本文介紹了所有這些內容,如下所示:
1. flip(int value):此方法刪除參數中指定的值。
public void flip(int value) 參數: value: the value to flip. Return ValueThis method does not return a value.
// Java code to demonstrate the
// working of flip(int value) in Bitset
import java.util.*;
public class Flip1 {
public static void main(String[] args) {
// declaring bitset
BitSet bset = new BitSet(6);
// assigning values to bset
bset.set(0);
bset.set(1);
bset.set(2);
bset.set(3);
// printing the original set
System.out.println("The original bitset is:" + bset);
// using flip() to remove 2
bset.flip(2);
// printing final bitset
// 2 is removed
System.out.println("The flipped bitset is:" + bset);
}
}
輸出:
The original bitset is:{0, 1, 2, 3} The flipped bitset is:{0, 1, 3}
2. flip(int fromnum,int tonum):此方法將指定的fromnum(包括)到指定的tonum(不包括)中的每個位設置為其當前值的補碼,即將fromnum刪除為tonum-1的值。
public void flip(int fromnum,int tonum) 參數: fromnum: start number to begin flipping. tonum: last-1 number to end flipping. 返回值: This method does not return a value.
// Java code to demonstrate the
// working of flip(int fromnum, int tonum) in Bitset
import java.util.*;
public class Flip2 {
public static void main(String[] args) {
// declaring bitset
BitSet bset = new BitSet(6);
// assigning values to bset
bset.set(0);
bset.set(1);
bset.set(2);
bset.set(3);
// printing the original set
System.out.println("The original bitset is:" + bset);
// using flip(fromnum,tonum) to remove 1 and 2
bset.flip(1,3);
// printing final bitset
// 1 and 2 are removed
System.out.println("The flipped bitset is:" + bset);
}
}
輸出:
The original bitset is:{0, 1, 2, 3} The flipped bitset is:{0, 3}
相關用法
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.BitSet.flip() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。