Java array with declaration of values โ€‹โ€‹around 20000

I am facing a problem with a Java array with roughly 20,000 values,

The complete 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 am trying to call this with:

String temp = UsableIDs.checkID( xyz );

      

Here at this moment the program always crashes Crash

I am using NetBeans.

I will be glad if someone can help me get this piece of code running.

+3


source to share


3 answers


You see the error because Java limits the size of static initializer blocks to 65535 bytes. Your best bet is to separate your data from your source code i.e. Put identifiers in a separate file.

The code below is equivalent to yours if the data in the array is stored (comma separated, no parentheses or line breaks) in /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";
    }
}

      

+2


source


Ok, I tested it myself ... The error it throws is Error:(13, 30) java: code too large

caused by array initialization which is too long. If you need a lot of elements, you'd be better off creating them through a loop or by reading them from a file.

The ids are in a 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";
    }
}

      

+2


source


static initialization has a limit of 65535 bytes. So this is giving you an error. Instead, you can put it in a file and read it into an array. enter image description here

+1


source







All Articles