Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java array with around 20000 values declaration

Tags:

java

arrays

I encounter an issue with Java array with around 20000 values,

Full Array can be found here

My Code:

public class UsableIDs {

    private final static int idsArray[] = {615,616,617,618,...};
    private final static int ids = idsArray.length;

    public static String checkID(int x){
        for(int i=0;i<ids;i++){
            if(x==idsArray[i])
                return "Usable";
        }
        return "NotUsable";
    }
}

In my other method I try to call this with:

String temp = UsableIDs.checkID( xyz );

Here at this point does the Program crash always Crash

I use NetBeans.

I'll be happy if someone can help me to get this Piece of Code running.

like image 837
LosFaul Avatar asked Jul 18 '26 16:07

LosFaul


2 Answers

You are seeing the error because Java limits the size of static initializer blocks to 65535 bytes. It would be best to separate your data from the source code, i.e. put the ids into a separate file.

The below code is equivalent to yours, assuming the data within the array is stored (comma-separated, without brackets or line breaks) into /temp/usableids.txt.

import java.nio.file.Files;
import java.nio.file.Paths;

public class UsableIDs {

    private final static int idsArray[];

    static {
        try {
            String fileData = new String(
                Files.readAllBytes(Paths.get("/temp/usableids.txt")), "UTF-8").trim();
            String[] ids = fileData.split(",");
            idsArray = new int[ids.length];

            for (int i = 0; i < ids.length; i++) {
                idsArray[i] = Integer.parseInt(ids[i]);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String checkID(int x) {
        for (int i = 0; i < idsArray.length; i++) {
            if (x == idsArray[i])
                return "Usable";
        }
        return "NotUsable";
    }
}
like image 71
Mick Mnemonic Avatar answered Jul 20 '26 05:07

Mick Mnemonic


Okay, I've tested it myself... The error that it's throwing is Error:(13, 30) java: code too large and is caused by the array initialization, which is too long. If you need that many elements, you'd be better off by generating them through a loop or reading them from a file.

The ids are in the range 615 - 18630, so the following code should do the trick:

public class UsableIDs {

    private final static int LOWER_BOUND= 615;
    private final static int UPPER_BOUND= 18630;

    private final static int idsArray[] = generateIds();
    private final static int ids = idsArray.length;

    private static int[] generateIds() {
        int length = UPPER_BOUND - LOWER_BOUND + 1;
        int[] array = new int[length];
        for(int i = 0; i < length; i++) {
            array[i] = LOWER_BOUND+ i;
        }
        return array;
    }

    public static String checkID(int x){
        for(int i=0;i<ids;i++){
            if(x==idsArray[i])
                return "Usable";
        }
        return "NotUsable";
    }
}
like image 37
QBrute Avatar answered Jul 20 '26 04:07

QBrute



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!