[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

array of bigintegers

Hi,
I want to create an array of 1000000 objects. Each of these objects is itself BigInteger.
for ex
list a[index] = new BigInteger("84512738249832");
but the index has to be an integer.
How can i create a list of 1000000 objects?
Thanks,
Anu
0
anumit
Asked:
anumit
  • 2
2 Solutions
 
CEHJCommented:
>>I want to create an array of 1000000 objects

BigInteger ii = new BigInteger[1000000];
0
 
VolatileVoidCommented:
Actually it's:
BigInteger[] ii = new BigInteger [1000000];

But this isn't enough. You've only allocated memory for the array but not for each object in the array. For example, if you try to now say:
ii[0].add ("1234567891223444");
You'll get a NullPointerException.

If you MUST use an array (and not, say, an ArrayList object) you MUST new each member before trying to manipulate it. A simple way would be:
for (int i = 0; i < ii.length; i++) ii[i] = new BigInteger ("0"); // initialize the array to 0.

Or you can "new" as needed, since the above causes the VM to allocate a huge portion of memory all at once.
0
 
CEHJCommented:
>>
 Actually it's:
BigInteger[] ii = new BigInteger [1000000];
>>

Correct. THanks for pointing out the typo. See my comment in your next question about initializing it
0
 
neonlinesCommented:
It's not a good practice of allocating memory for 1000000 BigInteger objects at first.

Instead of that u can use ArrayList as follows

ArrayList arrBigObject = new ArrayList();
arrBigObject .add(<ur big decimal object>);
...
...
...

U can proceed in that way and u have so many functions too in ArrayList for ur easy coding

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now