Java中BigInteger用法小結
在java中經常會遇到比較大的數(shù),甚至超過了long型,那么該如何處理這些“大數(shù)據(jù)”呢?在java中有兩個類BigInteger和BigDecimal分別表示大整數(shù)類和大浮點數(shù)類,從原則上是可以表示“天文單位”一樣大的數(shù)字咯,但有一個缺點就是比較費內存!
在這里,我們詳細描述下BigInteger的用法,在使用之前,我們需要導入java.math.*包
一.介紹BigInteger經常使用到的一些函數(shù)
①value.Of(參數(shù)); 這個函數(shù)的作用是將括號內的參數(shù)轉換成指定的數(shù)據(jù)類型,例如以下例子
int A=42;
BigInteger f=BigInteger.valueOf(A);
System.out.println("f="+f); //輸出的f將會等于BigInteger型的42
// 答案: f=42其實還可以轉成其他的類型,例如以下以下,※※※需要注意的是不重寫的話,jdk1.8 版本是無法支持這種轉換的※※※
String s="12345";
BigInteger c=BigInteger.valueOf(s);
// 則c=12345;※※※需要注意的是不重寫的話,jdk1.8 版本是無法支持這種轉換的
②add()方法; 這個函數(shù)的作用是將大整數(shù)加起來,例如以下例子
BigInteger c=new BigInteger("6");
BigInteger d=new BigInteger("3");
System.out.println("c+d="+c.add(d));
//答案輸出: c+d=9③subtract()方法,這個函數(shù)的作用是將大整數(shù)相減,例如以下例子,運用時前者減后者
BigInteger c=new BigInteger("5");
BigInteger d=new BigInteger("3");
System.out.println("d-c="+d.subtract(c));
//答案輸出: d-c=-2④multiply()方法,這個函數(shù)的作用是將大整數(shù)相乘,例如以下例子,
BigInteger c=new BigInteger("6");
BigInteger d=new BigInteger("3");
System.out.println("c*d="+c.multiply(d));
//答案輸出: c*d=18⑤divide()方法,這個函數(shù)的作用是將大整數(shù)做除法,例如以下例子,
BigInteger c=new BigInteger("6");
BigInteger d=new BigInteger("4");
System.out.println("c/d="+c.divide(d));
// 答案輸出;c/d=1⑥remainder()方法,這個函數(shù)的作用是將大整數(shù)取余
⑦pow(exponent)方法,這個函數(shù)的作用是將大整數(shù)取exponent的指數(shù),例如a.pow(b)==a^b;
⑧gcd()方法,這個函數(shù)的作用是將兩個大整數(shù)取最大公約數(shù),例如a.gcd(b);
⑨abs()方法,這個函數(shù)的作用是取絕對值,例如
BigInteger c=new BigInteger("-9");
System.out.println(c.abs());
//答案輸出: 9⑩negate()方法,這個函數(shù)的作用是取數(shù)的相反數(shù),例如
BigInteger c=new BigInteger("9");
System.out.println(c.negate());
// 答案輸出: -9?mod()方法; 這個函數(shù)的作用是對數(shù)進行取余 a.mod(b)=a%b=a.remainder(b);
?max()方法,min()方法,分別是比較兩個數(shù)的大小,例如a.max(b); 取a,b中的最大值
?compareTo()方法這個方法是用來比較兩個大整數(shù)大小的
public void testCompare() {
BigInteger bigNum1 = new BigInteger("52");
BigInteger bigNum2 = new BigInteger("27");
?
//1.compareTo():返回一個int型數(shù)據(jù)(1 大于; 0 等于; -1 小于)
int num = bigNum1.compareTo(bigNum2); //1
?
//2.max():直接返回大的那個數(shù),類型為BigInteger
// 原理:return (compareTo(val) > 0 ? this : val);
BigInteger compareMax = bigNum1.max(bigNum2); //52
?
//3.min():直接返回小的那個數(shù),類型為BigInteger
// 原理:return (compareTo(val) < 0 ? this : val);
BigInteger compareMin = bigNum1.min(bigNum2); //27
}?equals()方法,判斷兩個大整數(shù)是否相等,例如c.equals(d) 相等就返回 true;
二、介紹BigInteger的讀入方法——nextBigInteger(),從控制臺讀入一個BigInteger型數(shù)據(jù),類似于讀入int型的nextInt();
public void test() {
Scanner s = new Scanner(System.in); // 讀入
int n = sc.nextInt(); // 讀入一個int;
BigInteger m = sc.nextBigInteger(); // 讀入一個BigInteger;
while(sc.hasNext()){
System.out.print("sc.hasNext()=" + sc.hasNext());
}
}三、介紹BigInteger的構造方法
默認的是十進制,也就是我們平常較為常見的,例如
BigInteger d=new BigInteger("48");
System.out.println(d); //答案輸出:48,這里默認的是十進制,但也支持自定義轉換類型支持自定義進制類型(已存在的),例如二進制,四進制,八進制,十六進制,如下:
public void test() {
//在構造將函數(shù)時,把radix進制的字符串轉化為BigInteger
String str = "1011100111";
int radix = 2; // radix代表二進制,為下一行代碼中的參數(shù)radix賦值
BigInteger interNum1 = new BigInteger(str,radix); //743
}四、介紹BigInteger的幾個內部定義的常量——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一些基本類型的轉換
public void testToAnother() {
BigInteger bigNum = new BigInteger("38");
int radix = 2;
//1.轉換為bigNum的二進制補碼形式
byte[] num1 = bigNum.toByteArray();
//2.轉換為bigNum的十進制字符串形式
String num2 = bigNum.toString(); //38
//3.轉換為bigNum的radix進制字符串形式
String num3 = bigNum.toString(radix); //100110
//4.將bigNum轉換為int
int num4 = bigNum.intValue();
//5.將bigNum轉換為long
long num5 = bigNum.longValue();
//6.將bigNum轉換為float
float num6 = bigNum.floatValue();
//7.將bigNum轉換為double
double num7 = bigNum.doubleValue();
}六、權限控制
setBit(),testBit():可用于菜單的權限控制,非常好用,原理如下:
//權限控制: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ù)也不會溢出,而int最大值只有2147483647;
?
//2.取值驗證(返回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進去變量作為二進制數(shù),計算它們的和,并以十進制顯示; testBit():與setBit()相反,驗證this的二進制組成元素中是否包含傳入的變量;
//權限控制源碼分析:
//1.setBit()原理:計算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()原理:計算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;
}到此這篇關于Java中BigInteger用法的詳解的文章就介紹到這了,更多相關java中BigInteger用法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot整合apache ftpserver詳細教程(推薦)
這篇文章主要介紹了springboot整合apache ftpserver詳細教程,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
Java 中的 BufferedWriter 介紹_動力節(jié)點Java學院整理
BufferedWriter 是緩沖字符輸出流。它繼承于Writer。接下來通過本文給大家分享Java 中的 BufferedWriter知識,需要的朋友參考下吧2017-05-05
Spring Boot2.0整合ES5實現(xiàn)文章內容搜索實戰(zhàn)
這篇文章主要介紹了Spring Boot2.0整合ES5實現(xiàn)文章內容搜索實戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
postman測試post請求參數(shù)為json類型的實例講解
下面小編就為大家分享一篇postman測試post請求參數(shù)為json類型的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
SpringMVC中事務是否可以加在Controller層的問題
這篇文章主要介紹了SpringMVC中事務是否可以加在Controller層的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
微信小程序 navigator 跳轉url傳遞參數(shù)
這篇文章主要介紹了 微信小程序 navigator 跳轉url傳遞參數(shù)的相關資料,需要的朋友可以參考下2017-03-03
SpringBoot開發(fā)項目,引入JPA找不到findOne方法的解決
這篇文章主要介紹了SpringBoot開發(fā)項目,引入JPA找不到findOne方法的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
mybatis-plus QueryWrapper自定義查詢條件的實現(xiàn)
這篇文章主要介紹了mybatis-plus QueryWrapper自定義查詢條件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08

