java 大数据类型
2016-03-02 17:27
369 查看
大数加法
参考书籍发现使用使用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的详解如下:
将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=) 中的每一个运算符的各个方法,优先提供此方法。执行这些比较的建议语句是:(x.compareTo(y) <op > 0) ,其中 <op > 是六个比较运算符之一。
指定者:
接口
参数:
返回:
当此 BigInteger 在数值上小于、等于或大于 val 时,返回 -1,0,或 1。
参考书籍发现使用使用BigInteger可以解决这个问题。
于是查找了下JDK,然后测试几次终于写成功了!
使用心得如下:
1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了,于是总提示找不到提示符。
2,其构造方法有很多,但现在偶用到的有:
BigInteger(String val) 将 BigInteger 的十进制字符串表示形式转换为 BigInteger。 |
BigInteger(String val, int radix) 将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。 |
3,BigInteger类模拟了所有的int型数学操作,如add()==“+”,divide()==“-”等,但注意其内容进行数学运算时不能直接使用数学运算符进行运算,必须使用其内部方法。而且其操作数也必须为BigInteger型。
如:two.add(2)就是一种错误的操作,因为2没有变为BigInteger型。
4,当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串,详细说明如下:
String | toString() 返回此 BigInteger 的十进制字符串表示形式。 |
5,另外说明三个个用到的函数。
BigInteger | remainder(BigInteger val) 返回其值为 (this % val) 的 BigInteger。 |
BigInteger | negate() 返回其值是 (-this) 的 BigInteger。 |
int | compareTo(BigInteger val) 将此 BigInteger 与指定的 BigInteger 进行比较。 |
negate将操作数变为相反数。
compare的详解如下:
compareTo
public int compareTo ( BigInteger val)
将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=) 中的每一个运算符的各个方法,优先提供此方法。执行这些比较的建议语句是:(x.compareTo(y) <op > 0) ,其中 <op > 是六个比较运算符之一。
指定者:
接口
Comparable<BigInteger>中的
compareTo
参数:
val- 将此 BigInteger 与之比较的 BigInteger。
返回:
当此 BigInteger 在数值上小于、等于或大于 val 时,返回 -1,0,或 1。
相关文章推荐
- mbed OS - ARM关于物联网(IoT)的战略布局
- 解决 VC2012 ATL 多线程调用 报错:E_UNEXPECTED Catastrophic failure.
- 理解assign,copy,retain
- sqoop连接mysql出现Failed to list databases com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
- 使用Angular开发“云平台控制台”的实践总结
- 76.You need to display the first names of all customers from the CUSTOMERS table that contain the
- pod的SDK报错,Linker command failed with exit code1(use -v to see invocation)
- pod的SDK报错,Linker command failed with exit code1(use -v to see invocation)
- scala case匹配值
- AIX 6.1 异步 I/O 的 配置与性能评估
- 使用RAID进行磁盘管理
- AIX 6.1 异步 I/O 的 配置与性能评估
- studio中install_failed_dexopt 解决
- 环信SDK报错处理方法obtain an updated library from the vendor, or disable bitcode for this target. for archit
- 环信SDK报错处理方法obtain an updated library from the vendor, or disable bitcode for this target. for archit
- Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift 论文翻译
- io.h头文件下的一些函数 http://blog.csdn.net/babygjx/article/details/5740038
- 设计模式 ( 十二 ) 职责链模式(Chain of Responsibility)(对象行为)
- 对于大数据的理解
- .NET程序连接OPC Server时出错 对 COM 组件的调用返回了错误 HRESULT E_FAIL