简体   繁体   English

在Java中的biginteger数组中分配值

[英]assigning values in biginteger array in Java

For some program I need an array which stores a Fibonacci sequence number up to the 100th term. 对于某些程序,我需要一个数组,该数组存储斐波那契数列直到第100个词。 Although I can calculate that using fib function and storing in array, I want to save time by storing it manually. 尽管我可以使用fib函数计算并存储在数组中,但是我想通过手动存储来节省时间。 How can i do this? 我怎样才能做到这一点?

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”。

You have an BigInteger array and try to add primitive int types. 您有一个BigInteger数组,并尝试添加原始int类型。 You have to create BigInteger instances which can be part of your array: 您必须创建可以作为数组一部分的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: 或者,如果您的值大于一个长值,请使用带有String参数的构造函数:

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

int is a primitive type, while BigInteger is a class type. int是原始类型,而BigInteger是类类型。

For your case, you have an Array of BigIntegers. 对于您的情况,您有一个BigIntegers数组。 So each element of the array must be of the BigInteger class type. 因此,数组的每个元素都必须是BigInteger类类型。 You are giving it int, a primitive type, so it will complain. 您将其赋予int(原始类型),因此它将抱怨。

To solve this, each indice of the array should contain an object of the BigInteger type. 要解决此问题,数组的每个索引都应包含BigInteger类型的对象。

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

Where # is a number, and ... is additional indices. 其中#是数字,而...是其他索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM