How can I initialize a String array with length 0 in Java?

后端 未结 6 1799
悲哀的现实
悲哀的现实 2020-12-23 00:27

The Java Docs for the method
String[] java.io.File.list(FilenameFilter filter)
includes this in the returns description:

The arra

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 00:40

    Make a function which will not return null instead return an empty array you can go through below code to understand.

        public static String[] getJavaFileNameList(File inputDir) {
        String[] files = inputDir.list(new FilenameFilter() {
            @Override
            public boolean accept(File current, String name) {
                return new File(current, name).isFile() && (name.endsWith("java"));
            }
        });
    
        return files == null ? new String[0] : files;
    }
    

提交回复
热议问题