有没有办法使用 BigInteger 进行迭代?
BigInteger
是否只有 equals
进行比较?
是否有数学符号的替代品,例如 < (大于)或 < (少于)? ^已回答!
现在我想知道,有没有办法在迭代中使用 BigInteger
,例如 while
和 for
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 compareTo 方法。
You can use the compareTo method.
你不能使用数学符号,事实上我也不会养成使用 == 的习惯,我相当确定,除非他们使用一些严重的诡计,否则它会失败。
对于这类事情来说,Java 并不是一种很好的语言——我真的很喜欢 Java,但最终在实现一个 Complex 类,然后实现一个可以保存和操作该复杂类的矩阵时遇到了很多麻烦。
我的解决方案是使用 Java 创建核心类,然后使用 Groovy 创建使用核心类的类。如果遵循某些命名模式,则可以在任何类上使用任何运算符。
或者,如果您只是想处理大数字,只需使用 groovy,甚至不需要为变量声明类型 - 它会自动将它们提升为您需要的任何类型。
You can't use math notation, in fact I wouldn't get in the habit of using == either, I'm fairly sure that unless they used some serious trickery it will fail.
Java isn't a great language for this kind of stuff--I really love Java but ended up having a lot of trouble implementing a Complex class and then implementing a matrix that could hold and manipulate the complex class.
My solution was to use Java to create the core classes then use Groovy to create the classes that used the core classes. If you follow certain naming patterns, then you can use any operators on any class.
Or if you just want to mess with big numbers simply use groovy and don't even declare a type for your variables--it will automatically promote them to whatever you need.
Java 运算符仅设计用于对原始数据类型进行操作;他们不上课。由于BigInteger是一个类,算术比较和运算只能通过类方法来完成。
Java operators are only designed to only operate on primitive data types; they do not act on classes. Since BigInteger is a class, arithmetic comparisons and operations can only be done through class methods.