java:在 BigInteger 的情况下 for 循环如何工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3024186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java: how for loop work in the case of BigInteger
提问by Sanjeev
I want to take Input from the user as Big-Integer and manipulate it into a For loop
我想将用户输入作为 Big-Integer 并将其操作为 For 循环
BigInteger i;
for(BigInteger i=0; i<=100000; i++) {
System.out.println(i);
}
But it won't work
但它不会工作
can any body help me.
有谁能够帮助我。
采纳答案by polygenelubricants
You use these syntax instead:
您可以改用这些语法:
BigInteger i = BigInteger.valueOf(100000L); // long i = 100000L;
i.compareTo(BigInteger.ONE) > 0 // i > 1
i = i.subtract(BigInteger.ONE) // i = i - 1
So here's an example of putting it together:
所以这里有一个把它放在一起的例子:
for (BigInteger bi = BigInteger.valueOf(5);
bi.compareTo(BigInteger.ZERO) > 0;
bi = bi.subtract(BigInteger.ONE)) {
System.out.println(bi);
}
// prints "5", "4", "3", "2", "1"
Note that using BigInteger
as a loop index is highly atypical. long
is usually enough for this purpose.
请注意,BigInteger
用作循环索引是非常不典型的。long
通常足以达到这个目的。
API links
接口链接
The compareTo
idiom
该compareTo
成语
From the documentation:
从文档:
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.
对于六个布尔比较运算符(
<
、==
、>
、>=
、!=
、<=
)中的每一个,此方法优先于单独的方法提供。执行这些比较的建议习语是:( ),其中是六个比较运算符之一。x.compareTo(y)
<op>
0
<op>
In other words, given BigInteger x, y
, these are the comparison idioms:
换句话说,给定BigInteger x, y
,这些是比较习语:
x.compareTo(y) < 0 // x < y
x.compareTo(y) <= 0 // x <= y
x.compareTo(y) != 0 // x != y
x.compareTo(y) == 0 // x == y
x.compareTo(y) > 0 // x > y
x.compareTo(y) >= 0 // x >= y
This is not specific to BigInteger
; this is applicable to any Comparable<T>
in general.
这不是特定于BigInteger
; 这适用于任何Comparable<T>
一般情况。
Note on immutability
关于不变性的注意事项
BigInteger
, like String
, is an immutable object. Beginners tend to make the following mistake:
BigInteger
,就像String
,是一个不可变的对象。初学者容易犯以下错误:
String s = " hello ";
s.trim(); // doesn't "work"!!!
BigInteger bi = BigInteger.valueOf(5);
bi.add(BigInteger.ONE); // doesn't "work"!!!
Since they're immutable, these methods don't mutate the objects they're invoked on, but instead return new objects, the results of those operations. Thus, the correct usage is something like:
由于它们是不可变的,这些方法不会改变调用它们的对象,而是返回新对象,即这些操作的结果。因此,正确的用法类似于:
s = s.trim();
bi = bi.add(BigInteger.ONE);
回答by vodkhang
I think this code should work
我认为这段代码应该有效
public static void main(String[] args) {
BigInteger bigI = new BigInteger("10000000");
BigInteger one = new BigInteger("1");
for (; bigI.compareTo(one) == 0; bigI.subtract(one)) {
bigI = bigI.add(one);
}
}
回答by Justin K
Well, first of all, you have two variables called "i".
好吧,首先,您有两个变量,称为“i”。
Second, where's the user input?
其次,用户输入在哪里?
Third, i=i+i unboxes i into a primitive value, possibly overflowing it, and boxes the result in a new object (that is, if the statement even compiles, which I haven't checked).
第三, i=i+i 将 i 拆箱成一个原始值,可能会溢出它,并将结果装箱到一个新对象中(也就是说,如果语句甚至编译,我还没有检查过)。
Fourth, i=i+i can be written as i = i.multiply(BigInteger.valueof(2)).
第四,i=i+i 可以写成 i = i.multiply(BigInteger.valueof(2))。
Fifth, the loop is never run, because 100000 <= 1 is false.
第五,循环永远不会运行,因为 100000 <= 1 是假的。