在Java中分配biginteger数组中的值

时间:2021-12-31 16:48:43

For some program I need an array which stores a Fibonacci sequence number up to the 100th term. Although I can calculate that using fib function and storing in array, I want to save time by storing it manually. How can i do this?

对于某些程序,我需要一个存储斐波纳契序列号的数组,直到第100个项。虽然我可以使用fib函数计算并存储在数组中,但我希望通过手动存储来节省时间。我怎样才能做到这一点?

I was trying to do this like so:

我试图这样做:

BigInteger[] arr={259695496911122585,420196140727489673,
679891637638612258,1100087778366101931,1779979416004714189};

However, I get the error "Type mismatch; can not convert int to BigInteger".

但是,我得到错误“类型不匹配;无法将int转换为BigInteger”。

2 个解决方案

#1


2  

You have an BigInteger array and try to add primitive int types. You have to create BigInteger instances which can be part of your array:

你有一个BigInteger数组并尝试添加原始int类型。您必须创建可以成为数组一部分的BigInteger实例:

BigInteger[] arr={BigInteger.valueOf(1),BigInteger.valueOf(259695496911122585L)};

Or if your values are bigger as a long value use the constructor with String argument:

或者,如果您的值作为long值更大,请使用带String参数的构造函数:

BigInteger[] arr={new BigInteger("259695496911122585"),new BigInteger("420196140727489673")};

#2


1  

int is a primitive type, while BigInteger is a class type.

int是基本类型,而BigInteger是类类型。

For your case, you have an Array of BigIntegers. So each element of the array must be of the BigInteger class type. You are giving it int, a primitive type, so it will complain.

对于您的情况,您有一个BigIntegers数组。因此,数组的每个元素必须是BigInteger类类型。你给它int,一个原始类型,所以它会抱怨。

To solve this, each indice of the array should contain an object of the BigInteger type.

要解决此问题,数组的每个indice都应包含BigInteger类型的对象。

BigInteger[] bigIntegerArray = {new BigInteger(#),.....}

Where # is a number, and ... is additional indices.

其中#是数字,而......是附加索引。

#1


2  

You have an BigInteger array and try to add primitive int types. You have to create BigInteger instances which can be part of your array:

你有一个BigInteger数组并尝试添加原始int类型。您必须创建可以成为数组一部分的BigInteger实例:

BigInteger[] arr={BigInteger.valueOf(1),BigInteger.valueOf(259695496911122585L)};

Or if your values are bigger as a long value use the constructor with String argument:

或者,如果您的值作为long值更大,请使用带String参数的构造函数:

BigInteger[] arr={new BigInteger("259695496911122585"),new BigInteger("420196140727489673")};

#2


1  

int is a primitive type, while BigInteger is a class type.

int是基本类型,而BigInteger是类类型。

For your case, you have an Array of BigIntegers. So each element of the array must be of the BigInteger class type. You are giving it int, a primitive type, so it will complain.

对于您的情况,您有一个BigIntegers数组。因此,数组的每个元素必须是BigInteger类类型。你给它int,一个原始类型,所以它会抱怨。

To solve this, each indice of the array should contain an object of the BigInteger type.

要解决此问题,数组的每个indice都应包含BigInteger类型的对象。

BigInteger[] bigIntegerArray = {new BigInteger(#),.....}

Where # is a number, and ... is additional indices.

其中#是数字,而......是附加索引。