Imagine an instance of BigInteger
, then how to initialize it after creating instance?
想象一个BigInteger实例,那么如何在创建实例后初始化它?
For example:
例如:
BigInteger t = new BigInteger();
How to put a value in t
?
如何在t中赋值?
If the constructor cannot be called, then what can be done, to put the value in the object?
如果不能调用构造函数,那么可以做什么,将值放在对象中呢?
4 个解决方案
#1
9
I'm not 100% sure of what specifically is confusing you as you'd initialize the items in the BigInteger array as you would any other object array. e.g.,
我不能百分之百地确定什么具体会让你迷惑,因为你会在BigInteger数组中初始化项目,就像你会对其他对象数组一样。例如,
BigInteger t2 [] = new BigInteger[2];
t2[0] = new BigInteger("2");
t2[1] = BigInteger.ZERO; // ZERO, ONE, and TEN are defined by constants
// or
BigInteger[] t3 = {new BigInteger("2"), BigInteger.ZERO};
Edit 1:
Ah, now I understand your problem: you want to create a BigInteger instance and then later set its value. The answer is the same as for Strings: you can't, and that it is because BigIntegers like Strings are immutable and can't be changed once created. For this reason the class has no "setter" methods. The way to change the value of a BigInteger variable is to set it to a new BigInteger instance.
编辑1:啊,现在我理解了您的问题:您希望创建一个BigInteger实例,然后再设置它的值。答案和字符串是一样的:你不能,因为像字符串这样的大整数是不可变的,一旦创建就不能改变。由于这个原因,类没有“setter”方法。更改BigInteger变量的值的方法是将其设置为新的BigInteger实例。
#2
5
To convert a long (or a regular integer) to BigInteger, use the static factory method valueOf. The call BigInteger.valueOf(<i>someInteger</i>)
returns a new BigInteger
object holding the integer value you specify. You could also use new BigInteger("" + <i>someInteger</i>)
to get the same thing, but this is clunkier.
要将长(或常规整数)转换为BigInteger,请使用静态factory方法valueOf。调用BigInteger.valueOf(someInteger)返回一个新的BigInteger对象,该对象包含您指定的整数值。您也可以使用新的BigInteger(“”+ someInteger)来获得相同的东西,但这是比较笨拙的。
#3
3
here are some examples:
下面是一些例子:
BigInteger t = BigInteger.valueOf(23);
int i = 66;
t = BigInteger.valueOf(i);
t = BigInteger.ZERO
#4
0
I did something like this
我做了这样的事
//initialize with zero
BigInteger t = BigInteger.ZERO;
//if i is any value that is to be assigned
t=t.add(BigInteger.valueOf(i));
#1
9
I'm not 100% sure of what specifically is confusing you as you'd initialize the items in the BigInteger array as you would any other object array. e.g.,
我不能百分之百地确定什么具体会让你迷惑,因为你会在BigInteger数组中初始化项目,就像你会对其他对象数组一样。例如,
BigInteger t2 [] = new BigInteger[2];
t2[0] = new BigInteger("2");
t2[1] = BigInteger.ZERO; // ZERO, ONE, and TEN are defined by constants
// or
BigInteger[] t3 = {new BigInteger("2"), BigInteger.ZERO};
Edit 1:
Ah, now I understand your problem: you want to create a BigInteger instance and then later set its value. The answer is the same as for Strings: you can't, and that it is because BigIntegers like Strings are immutable and can't be changed once created. For this reason the class has no "setter" methods. The way to change the value of a BigInteger variable is to set it to a new BigInteger instance.
编辑1:啊,现在我理解了您的问题:您希望创建一个BigInteger实例,然后再设置它的值。答案和字符串是一样的:你不能,因为像字符串这样的大整数是不可变的,一旦创建就不能改变。由于这个原因,类没有“setter”方法。更改BigInteger变量的值的方法是将其设置为新的BigInteger实例。
#2
5
To convert a long (or a regular integer) to BigInteger, use the static factory method valueOf. The call BigInteger.valueOf(<i>someInteger</i>)
returns a new BigInteger
object holding the integer value you specify. You could also use new BigInteger("" + <i>someInteger</i>)
to get the same thing, but this is clunkier.
要将长(或常规整数)转换为BigInteger,请使用静态factory方法valueOf。调用BigInteger.valueOf(someInteger)返回一个新的BigInteger对象,该对象包含您指定的整数值。您也可以使用新的BigInteger(“”+ someInteger)来获得相同的东西,但这是比较笨拙的。
#3
3
here are some examples:
下面是一些例子:
BigInteger t = BigInteger.valueOf(23);
int i = 66;
t = BigInteger.valueOf(i);
t = BigInteger.ZERO
#4
0
I did something like this
我做了这样的事
//initialize with zero
BigInteger t = BigInteger.ZERO;
//if i is any value that is to be assigned
t=t.add(BigInteger.valueOf(i));