0

I have a Java app that has an array of 2.6 million values which never change. What is the best way to initialize it?

The obvious answer is to put it in code - i.e. something like:

int[] BigArray = new int[] {1, 2, 3, <...>, 2599999, 2600000};

but I am under the impression that this violates some size limit.

I have seen this post that mentions key/value pairs in Properties, but that doesn't seem particularly intuitive - I don't see why I need to have each array entry saved as a key (for the array index) and a value; I should be able to have just the values in order.

The next best way appears to be having the data saved as a file, and then to load the file into the array (or use a List) on startup, with each value on its own line (yes, the line separators take up 2.6MB, but so would 2.6 million commas). Is this a time-intensive way of doing it?

Community
  • 1
  • 1
Don Del Grande
  • 411
  • 6
  • 20
  • Are those the actual values, in order? If so, you don't even need the array, because `BigArray[i]` is `i + 1`. If you have `i`, just add `1`. – rgettman Apr 08 '14 at 18:30
  • is it just ints? and if so are all of them present or do you skip some? – dkatzel Apr 08 '14 at 18:30
  • 10
    I don't think this is a universal truth, but if you have that many constants in your application then you're doing something wrong. – Kon Apr 08 '14 at 18:33
  • If they are all `int`s then you don't have to convert them to text, you can just write them (as `int`s) to a file and read them. You can also serialize objects, if those are objects. – trutheality Apr 08 '14 at 18:35
  • No, thw array is not BigArray[N] = N for all N. The intent is to map 2.6 million values to about 135,000 values, each of which has one of 10 values - and there are about 20 sets of these 135,000 values, which the user can select. Having 20 sets of 2.6 million values would require the app to be 52 MB; 1 set of 2.6 million values and 20 of 130,000 uses only about 5.2 MB. – Don Del Grande Apr 08 '14 at 18:49
  • There is a limit to the size of an individual method, due to the (rather stupid) limits on the length of a branch. But this limit does not apply to the `static` method --- it can be enormous (and often is, in some of the JDKs char table classes, eg). So basically if you put the above initialization into `static` it might work. – Hot Licks Apr 08 '14 at 18:53
  • I would say you store those values with a separator like : (colon) in a string. Then parse them whenever you need them. If its not coming in one string, it should surely come in a set of strings. As length of 2^31 - 1 seems large enough to store your amount of data. http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method – bgth Apr 08 '14 at 19:42

1 Answers1

1

Assuming that you are trying to get an array of every int value up to 2.6M, just do this

    int[] bigArray = new int[2600000];
    for(int i = 0; i < 2600000; i++)
    {
        int j = i;
        bigArray[i] = j+1;
    }

The article you referenced was tagged Android development, so I don't know if you're developing for Android as this post is not tagged android, but this should work if you're doing what I think you're doing.

spb1994
  • 114
  • 8