Is there a way using BigInteger for iteration?
Does BigInteger
开发者_JAVA技巧only have equals
for comparison?
now i want to know, is there a way using BigInteger
in iteration such as while
and for
?
You can use the compareTo method.
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.
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 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 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.
精彩评论