Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to index an array of BigIntegers by BigIntegers

I'm trying to build an array of BigIntegers, but it seems like the array needs to be indexed by integers themselves (and if true, that seems extremely stupid to me but I'm hoping I'm just misunderstanding something). What I'm trying is the essentially the following:

BigInteger totalChoiceFunctions = BigInteger.valueOf(50031545098999704);
BigInteger[][] choiceFunctions = new BigInteger[totalChoiceFunctions][36];

But this causes the error "type mismatch: cannot convert from BigInteger to int". In order to remedy this I tried:

BigInteger[][] choiceFunctions = new BigInteger[totalChoiceFunctions.intValue()][36];

however this doesn't seem to help. When I compile and run I get the runtime error of:

exception in thread 'main' java.lang.NegativeArraySizeException

Confused, I looked at the oracle documentation for the intValue() method of BigInteger and found that "if this BigInteger is too big to fit in an int, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign". I suspect this is what's going on, considering that 50031545098999704 is certainly too big for int (and why I turned towards an array of BigIntegers since I want my array to be indexed by the numbers from 1 to 50031545098999704).

If my understanding is correct, then:

BigInteger[][] chioceFunctions = new BigInteger[totalChoiceFunctions][36];

creates an array which stores BigIntegers but is still indexed by ints. How can I make an array that both stores and is indexed by BigIntegers? Is it possible? Note that the code I'm using this for might in this case be able to work if I use longs rather than ints to index, but I want it to scale to a size where I'll be forced to index by BigIntegers. Am I missing something obvious?

like image 317
BLP92 Avatar asked Jan 06 '23 18:01

BLP92


2 Answers

Arrays in java are not sparse, so your array would need about 200 000 terabyte (not including the referenced arrays/BigIntegers). So no, currently not possible. There are some plans to support long as index in arrays with maybe java 10 (certainly not java9).

I guess you actually want a sparse datastructure; a Map<BigInteger,BigInteger> or as you have a nested array Map<Tuple<BigInteger,Integer>, BigInteger> should work for you.

like image 170
k5_ Avatar answered Jan 08 '23 09:01

k5_


No, it is not possible. In java, all arrays are indexed by integers only.

like image 24
Nikem Avatar answered Jan 08 '23 08:01

Nikem