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

后端 未结 8 961
执念已碎
执念已碎 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 15:57

    Initialising the studentnames array is counting towards the size of the main method. As there are 9000 student names each name can only be about 7 characters before you'll run out of space. As the others have stated you need to reduce the size of method. You can split it into pieces as Pramod said but in this case the bulk of the method is actually data. I would do as Infiltrator says and split the names out into a separate file and just read it in your main. Something like commons-io can be used to get you to effectively the same position you're starting in.

    List namelist = FileUtils.readLines(new File("studentnames.txt"));
    String[] studentnames = namelist.toArray(new String[0]);
    

    You may find it useful to process the list rather than convert it to an array or alternatively you could use a LineIterator instead

    LineIterator it = FileUtils.lineIterator(file);
    try {
         while (it.hasNext()) {
             String line = it.nextLine();
             // do something with line
         }
     } finally {
         it.close();
     }
    

提交回复
热议问题