How to fix “The code of method .. is exceeding the 65535 bytes limit”?

后端 未结 8 944
执念已碎
执念已碎 2020-12-14 15:36

I have the following code:

public static void main(String[] args) {
    try {
        String[] studentnames = {
            /* this is an array of 9000 strin         


        
8条回答
  •  失恋的感觉
    2020-12-14 16:09

    Solution number 1:

    public static void main(String[] args) {
        int[] num ={100001,100002,.......,...};
    }
    

    Suppose the array num contains 100000 numbers, and your main function gives an error. In such scenario, you can split the above array in two parts. Now, you can declare 2 different arrays instead of just a single array. For example:

    static int[] num1 ={} and static int[] num2={}

    Declare num1 outside of the main function and num2 inside the main function where num2 will replace num inside the main function.

    All elements of the array num will be split into two subarrays num1 and num2; split in such a way that both a static and non static array could hold say 8000 and 2000 elements.

    The code inside the main method can handle all 10000 elements, an assumption only:

    if (n <= 8000) {
        System.out.println(" --->  " + num2[((int) n - 1)]);
    } else if (n > 8000 && n <= 10000) {
        n = n - 8000;
        System.out.println(" --->  " + num1[((int) n - 1)]);
    }
    

    So splitting that array or say dividing the elements in two parts of 8000 and 2000, could remove the error.

    Solution number 2:

    Put the array outside main method, both num1 and num2 are now static arrays but the 65535 bytes limit holds also for static initialised arrays. Split your elements, for example, you can split 10000 in 8000 and 2000, or as per you requirement in two static arrays outside the main method.

    This error is related to the storage of elements in long[] or int[] where the error occurs if the storage size surpasses the limit of 65535. In such a scenario go splitting the elements, for example if you are using long[] or int[] split them in suitable numbers till the error gets resolved.

    In other words, it is not mandatory to only split the array into 2 parts, you can divide them in 4 different sizes also or different equal parts. But again the solution to your problem may vary as per your requirement.

提交回复
热议问题