array of bigintegers

Posted on 2005-04-10
Last Modified: 2012-08-13
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?
Question by:anumit
    LVL 86

    Expert Comment

    >>I want to create an array of 1000000 objects

    BigInteger ii = new BigInteger[1000000];
    LVL 1

    Assisted Solution

    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.
    LVL 86

    Expert Comment

     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
    LVL 3

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now