欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中BigInteger用法小結(jié)

 更新時(shí)間:2023年03月07日 16:17:01   作者:碼界猿候  
這篇文章主要介紹了Java中BigInteger用法的詳解,在這里,我們?cè)敿?xì)描述下BigInteger的用法,在使用之前,我們需要導(dǎo)入java.math.*包,本文通過(guò)實(shí)例代碼相結(jié)合給大家詳細(xì)講解,需要的朋友可以參考下

在java中經(jīng)常會(huì)遇到比較大的數(shù),甚至超過(guò)了long型,那么該如何處理這些“大數(shù)據(jù)”呢?在java中有兩個(gè)類(lèi)BigInteger和BigDecimal分別表示大整數(shù)類(lèi)和大浮點(diǎn)數(shù)類(lèi),從原則上是可以表示“天文單位”一樣大的數(shù)字咯,但有一個(gè)缺點(diǎn)就是比較費(fèi)內(nèi)存!

在這里,我們?cè)敿?xì)描述下BigInteger的用法,在使用之前,我們需要導(dǎo)入java.math.*包

一.介紹BigInteger經(jīng)常使用到的一些函數(shù)

value.Of(參數(shù)); 這個(gè)函數(shù)的作用是將括號(hào)內(nèi)的參數(shù)轉(zhuǎn)換成指定的數(shù)據(jù)類(lèi)型,例如以下例子

                      int A=42;
                     BigInteger f=BigInteger.valueOf(A);
                      System.out.println("f="+f); //輸出的f將會(huì)等于BigInteger型的42
                       // 答案: f=42

其實(shí)還可以轉(zhuǎn)成其他的類(lèi)型,例如以下以下,※※※需要注意的是不重寫(xiě)的話,jdk1.8 版本是無(wú)法支持這種轉(zhuǎn)換的※※※

                       String s="12345";
                      BigInteger c=BigInteger.valueOf(s);
        //  則c=12345;※※※需要注意的是不重寫(xiě)的話,jdk1.8 版本是無(wú)法支持這種轉(zhuǎn)換的

add()方法; 這個(gè)函數(shù)的作用是將大整數(shù)加起來(lái),例如以下例子

                       BigInteger c=new BigInteger("6");
                       BigInteger d=new BigInteger("3");
                      System.out.println("c+d="+c.add(d));
                        //答案輸出: c+d=9

③subtract()方法,這個(gè)函數(shù)的作用是將大整數(shù)相減,例如以下例子,運(yùn)用時(shí)前者減后者

                       BigInteger c=new BigInteger("5");
                     BigInteger d=new BigInteger("3");
                      System.out.println("d-c="+d.subtract(c));
                        //答案輸出: d-c=-2

④multiply()方法,這個(gè)函數(shù)的作用是將大整數(shù)相乘,例如以下例子,

                      BigInteger c=new BigInteger("6");
                     BigInteger d=new BigInteger("3");
                    System.out.println("c*d="+c.multiply(d));
                        //答案輸出: c*d=18

⑤divide()方法,這個(gè)函數(shù)的作用是將大整數(shù)做除法,例如以下例子,

                       BigInteger c=new BigInteger("6");
                      BigInteger d=new BigInteger("4");
                     System.out.println("c/d="+c.divide(d));
                        // 答案輸出;c/d=1

⑥r(nóng)emainder()方法,這個(gè)函數(shù)的作用是將大整數(shù)取余

⑦pow(exponent)方法,這個(gè)函數(shù)的作用是將大整數(shù)取exponent的指數(shù),例如a.pow(b)==a^b;

⑧gcd()方法,這個(gè)函數(shù)的作用是將兩個(gè)大整數(shù)取最大公約數(shù),例如a.gcd(b);

⑨abs()方法,這個(gè)函數(shù)的作用是取絕對(duì)值,例如

                      BigInteger c=new BigInteger("-9");
                      System.out.println(c.abs());
                        //答案輸出: 9

⑩negate()方法,這個(gè)函數(shù)的作用是取數(shù)的相反數(shù),例如

                      BigInteger c=new BigInteger("9");
                     System.out.println(c.negate());
                        // 答案輸出: -9

?mod()方法; 這個(gè)函數(shù)的作用是對(duì)數(shù)進(jìn)行取余 a.mod(b)=a%b=a.remainder(b);

?max()方法,min()方法,分別是比較兩個(gè)數(shù)的大小,例如a.max(b); 取a,b中的最大值

?compareTo()方法這個(gè)方法是用來(lái)比較兩個(gè)大整數(shù)大小的

 public void testCompare() {
   BigInteger bigNum1 = new BigInteger("52");
   BigInteger bigNum2 = new BigInteger("27");
?
   //1.compareTo():返回一個(gè)int型數(shù)據(jù)(1 大于; 0 等于; -1 小于)
   int num = bigNum1.compareTo(bigNum2);           //1
?
   //2.max():直接返回大的那個(gè)數(shù),類(lèi)型為BigInteger
   //  原理:return (compareTo(val) > 0 ? this : val);
   BigInteger compareMax = bigNum1.max(bigNum2);   //52
?
   //3.min():直接返回小的那個(gè)數(shù),類(lèi)型為BigInteger
   //  原理:return (compareTo(val) < 0 ? this : val);
   BigInteger compareMin = bigNum1.min(bigNum2);   //27
}

?equals()方法,判斷兩個(gè)大整數(shù)是否相等,例如c.equals(d) 相等就返回 true;

二、介紹BigInteger的讀入方法——nextBigInteger(),從控制臺(tái)讀入一個(gè)BigInteger型數(shù)據(jù),類(lèi)似于讀入int型的nextInt();

public void test() {
    Scanner s = new Scanner(System.in);             // 讀入
    int n = sc.nextInt();                           // 讀入一個(gè)int;
    BigInteger m = sc.nextBigInteger();             // 讀入一個(gè)BigInteger;
    while(sc.hasNext()){    
        System.out.print("sc.hasNext()=" + sc.hasNext());
    }
}

三、介紹BigInteger的構(gòu)造方法

默認(rèn)的是十進(jìn)制,也就是我們平常較為常見(jiàn)的,例如

      BigInteger d=new BigInteger("48");
      System.out.println(d);  //答案輸出:48,這里默認(rèn)的是十進(jìn)制,但也支持自定義轉(zhuǎn)換類(lèi)型

支持自定義進(jìn)制類(lèi)型(已存在的),例如二進(jìn)制,四進(jìn)制,八進(jìn)制,十六進(jìn)制,如下:

public void test() {
   //在構(gòu)造將函數(shù)時(shí),把radix進(jìn)制的字符串轉(zhuǎn)化為BigInteger
   String str = "1011100111";
   int radix = 2;  // radix代表二進(jìn)制,為下一行代碼中的參數(shù)radix賦值
   BigInteger interNum1 = new BigInteger(str,radix);   //743
}

四、介紹BigInteger的幾個(gè)內(nèi)部定義的常量——BigInteger.ZERO,BigInteger.ONE,BigInteger.TEN

//之前是支持-1和2,但現(xiàn)在表明已不再輸出(Not exported.)
public void test() {
    //0
    BigInteger zero = BigInteger.ZERO;
    //1
    BigInteger one = BigInteger.ONE;
    //10
    BigInteger ten = BigInteger.TEN;
}

五、介紹BigInteger一些基本類(lèi)型的轉(zhuǎn)換

public void testToAnother() {
    BigInteger bigNum = new BigInteger("38");
    int radix = 2;
    
    //1.轉(zhuǎn)換為bigNum的二進(jìn)制補(bǔ)碼形式
    byte[] num1 = bigNum.toByteArray();
    //2.轉(zhuǎn)換為bigNum的十進(jìn)制字符串形式
    String num2 = bigNum.toString();        //38
    //3.轉(zhuǎn)換為bigNum的radix進(jìn)制字符串形式
    String num3 = bigNum.toString(radix);   //100110
    //4.將bigNum轉(zhuǎn)換為int
    int num4 = bigNum.intValue();
    //5.將bigNum轉(zhuǎn)換為long
    long num5 = bigNum.longValue();
    //6.將bigNum轉(zhuǎn)換為float
    float num6 = bigNum.floatValue();
    //7.將bigNum轉(zhuǎn)換為double
    double num7 = bigNum.doubleValue();
}

六、權(quán)限控制

setBit(),testBit():可用于菜單的權(quán)限控制,非常好用,原理如下:

//權(quán)限控制:setBit(),testBit()
@Test
public void testSetAndTest() {
    //1.封裝數(shù)據(jù)(setBit的值需 >= 0,否則出現(xiàn)異常:ArithmeticException("Negative bit address"))
    BigInteger permission = new BigInteger("0");
    BigInteger numBig = permission.setBit(2);
    numBig = numBig.setBit(5);
    numBig = numBig.setBit(13);
    numBig = numBig.setBit(66);
    System.out.println("原理:" + numBig); 
    // 原理:73786976294838214692 = 2^2+2^5+2^13+2^66 次方的和;
    // 看!!即使這么大的數(shù)也不會(huì)溢出,而int最大值只有2147483647;
?
    //2.取值驗(yàn)證(返回Boolean型)
    boolean flag1 = numBig.testBit(2);      //true
    boolean flag2 = numBig.testBit(5);      //true
    boolean flag3 = numBig.testBit(13);     //true
    boolean flag4 = numBig.testBit(66);     //true
    boolean flag5 = numBig.testBit(27);     //false
}

setBit():將set進(jìn)去變量作為二進(jìn)制數(shù),計(jì)算它們的和,并以十進(jìn)制顯示; testBit():與setBit()相反,驗(yàn)證this的二進(jìn)制組成元素中是否包含傳入的變量;

//權(quán)限控制源碼分析:
 
//1.setBit()原理:計(jì)算this與2的n次方的和
public BigInteger setBit(int n) {
	if (n < 0)
		throw new ArithmeticException("Negative bit address");
 
	int intNum = n >>> 5;
	int[] result = new int[Math.max(intLength(), intNum+2)];
 
	for (int i=0; i < result.length; i++)
		result[result.length-i-1] = getInt(i);
 
	result[result.length-intNum-1] |= (1 << (n & 31));
 
	return valueOf(result);
}
 
//2.testBit()原理:計(jì)算this的值中是否包含2的n次方
public boolean testBit(int n) {
	if (n < 0)
		throw new ArithmeticException("Negative bit address");
 
	return (getInt(n >>> 5) & (1 << (n & 31))) != 0;
}

到此這篇關(guān)于Java中BigInteger用法的詳解的文章就介紹到這了,更多相關(guān)java中BigInteger用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論