java 定义数组太长 出现java: code too large

今天练习时,使用一个长度为100000的int数组,

int[] arr = new int[]{9738,7398,634,3006,9955,5722,2078,756,110,6717,9136,4180,6412,1255,7432,3070,1189,4277,8632,9072,5492,7594,4236,7574,4284,3780,3192,1136,7929,7884,8842,2395,9076,3982,6903,5079,9432,7900,5357,7409,5159,8636,5924,9980,1888,9476,7277,3606,8920,2889,7374,8275,9939,5785,4928,6035,3291,7639,5709,9228,2568,4507,5701,2390,9258,393,8586,2972,2137,6451,1333,1668,7405,7311,9276,4651,5539,9658,2010,5817,4371,2847,3663,1656,58,8214,1366,7512,426,8896,2018,694,8605,8996,7141,8763,5856,8547,1125,6519,1666,442,7796,6664,6540,1332,8813,506,9629,9377,9882,7003,2271,9379,7336,845,5537,4353,5513,1677,863,2172,5951,6644,2009,4259,8559,4999,7254,6955,4963,614,3652,582,8020,5025,7596,8892,6754,4294,2116,4144,2204,4267,3321,6199,9479,6861,9222,5046,1034,3138,1255,4237,384,5532,424,7477,3011,3262,5760,977,5540,646,7047,5690,6202,8142,848,4337,9851,553,6902,1238,8888,1454,9758,8890,8177,9913,8187,9161,9159,5420,6865,9009,2739,7599,6221,9645,1398,8057,7892,1199,4478,4581,7623,1282,8966,8883,5142,6930,332,2361,9403,3687,43,935,9334,2779,298,286,531,3727,8930,5914,2732,124,3779,9851,8857,4647,7070,6331,782,706,7107,2966,6957,9511,8268,2639,2160,283,1426,1650,8571,7814,7435,7150,9858,1901,3014,2107,7071,169,9579,2225,2127,4292,2898,308,2749,1154,2813,823,4555,8927,893,2669,5172,19,1989,3453,5031,2030,4618,9191,5494,4160,8644,6123,9950,7721,577,2256,7691,790,7697,4316,6951,8624,8547,819,402,4685,4096,9393,7942,6901,9006,9138,7265,2051,1717,1929,1857,9205,7260,6217,5421,7606,1582,4715,6192,8937,2769,1985,8367,103,6875,2949,9375,9594,9042,1648,5457,7312,8951,7847,9146,5603,6176,8744,9986,6665,3042,9661,4253,2760,4119,8629,1425,4679,8360,7143,3743,5044,6890,1565,2942,9260,5835,3086,2974,2732,3920,2016,3764,719,8812,9530,2117,8482,1918,3338,1160,2096,675,4094,4179,8302,2189,584,8034,4019,7491,60,8651,4136,8611,2146,7762,4135,748,149,6665,8759,9281,2836,1797,9975,6402,7631,7571,9773,7477,3403,5382,5661,2326,2186,3825,1561,5399,9065,8991,493,3600,1172,3298,7435,6893,7562,9903,7756,118,6036,4150,5262,9589,9589,8713,4824,6921,3758,5105,954,8991,340,4273,6872,3230,2946,5225,3640,5923,4616,121,2740,3399,321,2154,1920,266,3859,8255,5429,6936,5411,102,2183,4495,3769,4554,543,5171,6842,6694,8610,6059,229,4274,1486,9479,6154,410,7068,6093,9658,9190,5155,8590,2417,9124,4284,1508,216,350,3757,1121,3166,5041,3538,580,4375,774,9166,9924,3798,2818,4805,8040,7780,9276,353,3462,8708,5320,6746,7621,8853,5569,9892,3599,1377,654,9124,3585,5460,1671,2153,6184,9840,402,4484,2949,8603,8405,1315,9552,7807,9508,5929,6747,7542,1662,6740,2634,2429,6202,6846,1879,4985,4400,5320,3195,5914,9570,5676,2996,4537,6186,4386,6172,9753,3765,883,5699,9585,8823,3251,348,7537,964,1680,858,6431,9477,4694,4068,7728,1238,9713,668,7656,5493,1060,4599,3879,8561,7011,6246,1318,5178,1771,6620,9707,6049,1622,1104,6234,940,7755,2249,3628,5362,930,7369,341,6191,4566,755,606,6313,6871,9752,9712,6906,7966,3488,1526,292,3399,9061,4940,4105,3685,9254,3949,4708,4677,9120,8499,933,5228,6295,9951,1109,2258,5204,7571,8517,72,8295,2094,6700,6787,6860,3583,5103,1247,1625,9239,6083,6662,6154...}

出现了java: code too large错误。原因不详,可能是由于这样定义的数组生成的字节码文件超过了限制的大小,网上没有搜到关于字节码文件限制大小的信息,可能跟不同的jvm实现有关。如果有人知道原因,请留言。

解决方案:

使用properties加载该数组。

1.在resources目录下创建Properties文件

 

 文件内容

 

 

2.代码中使用Properties类加载该文件

Properties properties = new Properties();
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("toolongarrays.properties");
properties.load(inputStream);
String res =(String) properties.get("array");
String[] arr = res.split(",");
int[] candiesCount = new int[arr.length];
for(int i = 0;i<arr.length;i++){
candiesCount[i]=Integer.parseInt(arr[i].replace("\"",""));
}

posted on 2021-06-01 14:39  jejas  阅读(890)  评论(0编辑  收藏  举报