如何正确传递函数参数?

时间:2021-08-07 10:44:35

Currently I have the code like below:

目前我的代码如下:

    public boolean accept(File dir, String filename) {
        ...
        return filename.contains(".png") || filename.contains(".jpg") || file.isDirectory();

(the full code can be found there - https://*.com/a/18523047/604388)

(完整的代码可以在那里找到 - https://*.com/a/18523047/604388)

So, .png and .jpg are hardcoded now. I would like to make it as function parameter(s) and allow to pass several extensions, not just one. How could I do it?

所以,.png和.jpg现在都是硬编码的。我想把它作为函数参数,并允许传递几个扩展,而不只是一个。我怎么能这样做?

I think I can pass list of extensions as array, but how could I perform all these checks (filename.contains()) for all array elements?

我想我可以将扩展列表作为数组传递,但是如何为所有数组元素执行所有这些检查(filename.contains())?

5 个解决方案

#1


0  

You can use the below snippet which should be enough for your needs :

您可以使用以下代码段,该代码段应该足以满足您的需求:

private boolean loadFileList(String directory, String...extensions){
    for (String extension : extensions){
        if (filename.contains(extension))
            return true;
    }
    return false;
}

P.S. I'm not sure what is the filename type, but if returns boolean then the above example should fit

附:我不确定文件名类型是什么,但是如果返回boolean则上面的例子应该适合

#2


2  

The easiest approach I can come up with is to iterate through array of extensions:

我能想到的最简单的方法是迭代扩展数组:

boolean res = false;
for (String ext: extensions) {
    if (filename.endsWith(ext)) {
        res = true;
    }
}

#3


1  

You should consider using an enum to avoid hardcoding.-

您应该考虑使用枚举来避免硬编码.-

public enum Extension {
    PNG     (".png"),
    JPG     (".jpg");

    private String suffix;

    Extension(String suffix) {
        this.suffix = suffix;
    }

    public String getSuffix() {
        return suffix;
    }
}

Your code would look like this.-

你的代码看起来像这样.-

public boolean accept(File dir, String filename, Extension... extensions) {
    boolean res = file.isDirectory();

    if (!res) {
        for (int i = 0; i < extensions.length; i ++) {
            res = res || filename.contains(extensions[i].getSuffix());
        }
    }

    return res;
}

#4


1  

No built-in answer here I fear, so iterate will be the good old solution:

我担心这里没有内置答案,所以迭代将是一个很好的旧解决方案:

boolean result = false;
for (String extension : extensions) {
    if (filename.contains(extension)) {
        result = true;
        break;
    }
}
return result || file.isDirectory();

extensions may be a String[] or a Collection<String>

扩展名可以是String []或Collection

Note : Better, yet, to use endsWith() rather than contains() for extensions. But I used the same condition as OP...

注意:更好的是,使用endsWith()而不是contains()来扩展。但我使用与OP相同的条件......

#5


0  

import java.io.File;
import java.io.FileFilter;

public class NewClass {
    public static File[] loadFileList(String dir, FileFilter filter){
        return new File(dir).listFiles(filter);
    }

    public static void loadFiles(){
        File files[] = loadFileList("c:\\", new FileFilter() {
            public boolean accept(File pathname) {
                if(pathname.getAbsolutePath().toLowerCase().endsWith(".png")){
                    return true;
                }
                if(pathname.getAbsolutePath().toLowerCase().endsWith(".jpg")){
                    return true;
                }
                return false;
            }
        });
        // here do some DIRTY things with files....
    }
}

#1


0  

You can use the below snippet which should be enough for your needs :

您可以使用以下代码段,该代码段应该足以满足您的需求:

private boolean loadFileList(String directory, String...extensions){
    for (String extension : extensions){
        if (filename.contains(extension))
            return true;
    }
    return false;
}

P.S. I'm not sure what is the filename type, but if returns boolean then the above example should fit

附:我不确定文件名类型是什么,但是如果返回boolean则上面的例子应该适合

#2


2  

The easiest approach I can come up with is to iterate through array of extensions:

我能想到的最简单的方法是迭代扩展数组:

boolean res = false;
for (String ext: extensions) {
    if (filename.endsWith(ext)) {
        res = true;
    }
}

#3


1  

You should consider using an enum to avoid hardcoding.-

您应该考虑使用枚举来避免硬编码.-

public enum Extension {
    PNG     (".png"),
    JPG     (".jpg");

    private String suffix;

    Extension(String suffix) {
        this.suffix = suffix;
    }

    public String getSuffix() {
        return suffix;
    }
}

Your code would look like this.-

你的代码看起来像这样.-

public boolean accept(File dir, String filename, Extension... extensions) {
    boolean res = file.isDirectory();

    if (!res) {
        for (int i = 0; i < extensions.length; i ++) {
            res = res || filename.contains(extensions[i].getSuffix());
        }
    }

    return res;
}

#4


1  

No built-in answer here I fear, so iterate will be the good old solution:

我担心这里没有内置答案,所以迭代将是一个很好的旧解决方案:

boolean result = false;
for (String extension : extensions) {
    if (filename.contains(extension)) {
        result = true;
        break;
    }
}
return result || file.isDirectory();

extensions may be a String[] or a Collection<String>

扩展名可以是String []或Collection

Note : Better, yet, to use endsWith() rather than contains() for extensions. But I used the same condition as OP...

注意:更好的是,使用endsWith()而不是contains()来扩展。但我使用与OP相同的条件......

#5


0  

import java.io.File;
import java.io.FileFilter;

public class NewClass {
    public static File[] loadFileList(String dir, FileFilter filter){
        return new File(dir).listFiles(filter);
    }

    public static void loadFiles(){
        File files[] = loadFileList("c:\\", new FileFilter() {
            public boolean accept(File pathname) {
                if(pathname.getAbsolutePath().toLowerCase().endsWith(".png")){
                    return true;
                }
                if(pathname.getAbsolutePath().toLowerCase().endsWith(".jpg")){
                    return true;
                }
                return false;
            }
        });
        // here do some DIRTY things with files....
    }
}