3

Does BigInteger only have equals for comparison?
Are there substitutes for math notation like < (greater than) or < (less than)? ^Answered!

now i want to know, is there a way using BigInteger in iteration such as while and for?

4

3 回答 3

5

You can use the compareTo method.

于 2011-01-26T06:32:39.110 回答
1

你不能使用数学符号,事实上我也不会养成使用 == 的习惯,我很确定除非他们使用了一些严重的诡计,否则它会失败。

a = new BigInteger(500);

b = a;
if( a == b ) 
    will always be true

b=new BigInteger(500);
if( a == b )
    will never be true

if( a.equals(b) )
    will always work fine.

对于这类东西,Java 不是一种很好的语言——我真的很喜欢 Java,但最终在实现一个复杂类然后实现一个可以保存和操作复杂类的矩阵时遇到了很多麻烦。

我的解决方案是使用 Java 创建核心类,然后使用 Groovy 创建使用核心类的类。如果您遵循某些命名模式,那么您可以在任何类上使用任何运算符。

或者,如果您只想处理大数字,只需使用 groovy,甚至不为您的变量声明类型——它会自动将它们提升为您需要的任何内容。

于 2011-01-26T07:00:20.643 回答
0

Java 运算符仅设计为仅对原始数据类型进行操作;他们不对班级采取行动。由于 BigInteger 是一个类,因此只能通过类方法进行算术比较和运算。

于 2011-01-26T18:47:22.623 回答