Java中BigInteger用法小結(jié)
在java中經(jīng)常會(huì)遇到比較大的數(shù),甚至超過(guò)了long型,那么該如何處理這些“大數(shù)據(jù)”呢?在java中有兩個(gè)類BigInteger和BigDecimal分別表示大整數(shù)類和大浮點(diǎn)數(shù)類,從原則上是可以表示“天文單位”一樣大的數(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ù)類型,例如以下例子
int A=42;
BigInteger f=BigInteger.valueOf(A);
System.out.println("f="+f); //輸出的f將會(huì)等于BigInteger型的42
// 答案: f=42其實(shí)還可以轉(zhuǎn)成其他的類型,例如以下以下,※※※需要注意的是不重寫的話,jdk1.8 版本是無(wú)法支持這種轉(zhuǎn)換的※※※
String s="12345";
BigInteger c=BigInteger.valueOf(s);
// 則c=12345;※※※需要注意的是不重寫的話,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ù),類型為BigInteger
// 原理:return (compareTo(val) > 0 ? this : val);
BigInteger compareMax = bigNum1.max(bigNum2); //52
?
//3.min():直接返回小的那個(gè)數(shù),類型為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ù),類似于讀入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)換類型支持自定義進(jìn)制類型(已存在的),例如二進(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一些基本類型的轉(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)文章
springboot整合apache ftpserver詳細(xì)教程(推薦)
這篇文章主要介紹了springboot整合apache ftpserver詳細(xì)教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Java 中的 BufferedWriter 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
BufferedWriter 是緩沖字符輸出流。它繼承于Writer。接下來(lái)通過(guò)本文給大家分享Java 中的 BufferedWriter知識(shí),需要的朋友參考下吧2017-05-05
Spring Boot2.0整合ES5實(shí)現(xiàn)文章內(nèi)容搜索實(shí)戰(zhàn)
這篇文章主要介紹了Spring Boot2.0整合ES5實(shí)現(xiàn)文章內(nèi)容搜索實(shí)戰(zhàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
淺談如何優(yōu)雅地停止Spring Boot應(yīng)用
這篇文章主要介紹了淺談如何優(yōu)雅地停止Spring Boot應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
postman測(cè)試post請(qǐng)求參數(shù)為json類型的實(shí)例講解
下面小編就為大家分享一篇postman測(cè)試post請(qǐng)求參數(shù)為json類型的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
SpringMVC中事務(wù)是否可以加在Controller層的問(wèn)題
這篇文章主要介紹了SpringMVC中事務(wù)是否可以加在Controller層的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
微信小程序 navigator 跳轉(zhuǎn)url傳遞參數(shù)
這篇文章主要介紹了 微信小程序 navigator 跳轉(zhuǎn)url傳遞參數(shù)的相關(guān)資料,需要的朋友可以參考下2017-03-03
SpringBoot開(kāi)發(fā)項(xiàng)目,引入JPA找不到findOne方法的解決
這篇文章主要介紹了SpringBoot開(kāi)發(fā)項(xiàng)目,引入JPA找不到findOne方法的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
mybatis-plus QueryWrapper自定義查詢條件的實(shí)現(xiàn)
這篇文章主要介紹了mybatis-plus QueryWrapper自定義查詢條件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
如何將Java與C#時(shí)間進(jìn)行互相轉(zhuǎn)換
這篇文章主要介紹了Java與C#時(shí)間互轉(zhuǎn)的方法以及JAVA日期、C#日期計(jì)算說(shuō)明,需要的朋友可以參考下2022-11-11

