java.math.BigInteger使用心得总结(转)
2014-08-11 09:42
381 查看
今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换。都会变成0.
参考书籍发现使用使用BigInteger可以解决这个问题。
于是查找了下JDK,然后测试几次终于写成功了!
使用心得如下:
1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了,于是总提示找不到提示符。
2,其构造方法有很多,但现在偶用到的有:
如要将int型的2转换为BigInteger型,要写为BigInteger two=new BigInteger("2"); //注意2双引号不能省略
3,BigInteger类模拟了所有的int型数学操作,如add()==“+”,divide()==“-”等,但注意其内容进行数学运算时不能直接使用数学运算符进行运算,必须使用其内部方法。而且其操作数也必须为BigInteger型。
如:two.add(2)就是一种错误的操作,因为2没有变为BigInteger型。
4,当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串,详细说明如下:
输出方法:System.out.print(two.toString());
5,另外说明三个个用到的函数。
remainder用来求余数。
negate将操作数变为相反数。
compare的详解如下:
Compares this BigInteger with the specified BigInteger. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
Specified by:
View Code
输出:
参考书籍发现使用使用BigInteger可以解决这个问题。
于是查找了下JDK,然后测试几次终于写成功了!
使用心得如下:
1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了,于是总提示找不到提示符。
2,其构造方法有很多,但现在偶用到的有:
BigInteger(Stringval) |
BigInteger(Stringval, int radix) |
3,BigInteger类模拟了所有的int型数学操作,如add()==“+”,divide()==“-”等,但注意其内容进行数学运算时不能直接使用数学运算符进行运算,必须使用其内部方法。而且其操作数也必须为BigInteger型。
如:two.add(2)就是一种错误的操作,因为2没有变为BigInteger型。
4,当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串,详细说明如下:
String | toString() 返回此 BigInteger 的十进制字符串表示形式。 |
5,另外说明三个个用到的函数。
BigInteger | remainder(BigIntegerval) |
BigInteger | negate() 返回其值是 (-this) 的 BigInteger。 |
int | compareTo(BigIntegerval) |
negate将操作数变为相反数。
compare的详解如下:
compareTo
public int compareTo(BigInteger val)
Compares this BigInteger with the specified BigInteger. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
Specified by:
compareToin interface
Comparable<BigInteger>Parameters:
val- BigInteger to which this BigInteger is to be compared.Returns:-1, 0 or 1 as this BigInteger is numerically less than, equal to, or greater than val.
import java.math.BigInteger; public class BigIntegerDemo { public static void main(String[] args) { BigInteger big=BigInteger.ONE; System.out.println("BigInteger.ONE:"+big); System.out.println("nextProbablePrime:"+big.nextProbablePrime()); System.out.println("nextProbablePrime:"+big.nextProbablePrime()); big=BigInteger.TEN; System.out.println("BigInteger.TEN:"+big); big=BigInteger.ZERO; System.out.println("BigInteger.ZERO:"+big); } }
View Code
输出:
BigInteger.ONE:1 nextProbablePrime:2 nextProbablePrime:2 BigInteger.TEN:10 BigInteger.ZERO:0
相关文章推荐
- java.math.BigInteger使用心得总结
- java.math.BigInteger使用心得总结
- java.math.BigInteger使用心得总结
- Java中正则表达式和常用的Math、Random、System、BigInteger、BigDecimal、Date/DateFormat、Calendar类的使用
- Java基础-14总结正则表达式,Pattern,Mactcher,Math,BigInteger,BigDeximal,System等
- Java基础-14总结正则表达式,Pattern,Mactcher,Math,BigInteger,BigDeximal,System等
- 【Java】-BigInteger大数类的使用【超强Java大数模板 总结】
- 以 java 为例,总结下 appium 里的一方法的使用心得
- 大数类java.math.BigInteger
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer
- MySQL分段统计SQL写法 与 Mybatis 异常 java.math.BigDecimal cannot be cast to java.lang.Integer
- JAVA 大数(BigInteger) 归纳总结
- 【正整数模板运算(C++手写BigInteger类和java.math.BigInteger以及java.math.BigDecimal)】
- 十三、java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
- 练习使用Java的BigInteger解决矩阵翻硬币问题
- java中进行高精度、大数运算总结BigInteger BigDecimal
- JAVA 大数(BigInteger) 归纳总结 .
- java.math.BigInteger类型的数据转成int
- java.math.BigInteger类型的数据转成int
- 【java基础】大数据的处理:java.math.Number/BigInteger/BigDecimal