Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查文件夹中文件名中的元素是否包含字符串列表中的字符串名?_Java_Arrays_Arraylist_Directory - Fatal编程技术网

Java 如何检查文件夹中文件名中的元素是否包含字符串列表中的字符串名?

Java 如何检查文件夹中文件名中的元素是否包含字符串列表中的字符串名?,java,arrays,arraylist,directory,Java,Arrays,Arraylist,Directory,我正在尝试搜索一个图像文件夹,以检查是否每个图像 文件夹中的路径包含my ArrayList中的字符串元素。如果 文件路径包含字符串元素,我想将其添加到新的 字符串数组。这就是我目前所拥有的。匹配的图像路径 数组列表中的字符串在for循环中正确打印,但是 除此之外,它打印空值 /*ArrayList content: Snow Fall.jpg Soft snow.jpg Winter Wonderland.jpg */ public void getImages(Arr

我正在尝试搜索一个图像文件夹,以检查是否每个图像 文件夹中的路径包含my ArrayList中的字符串元素。如果 文件路径包含字符串元素,我想将其添加到新的 字符串数组。这就是我目前所拥有的。匹配的图像路径 数组列表中的字符串在for循环中正确打印,但是 除此之外,它打印空值

/*ArrayList content:
   Snow Fall.jpg
   Soft snow.jpg 
   Winter Wonderland.jpg
*/


public void getImages(ArrayList<String> theImagesArrayList) {

    File dir = new File("C:/Users/someone/Desktop/slideshow images");

    int count=0;
    File[] files = dir.listFiles();
    file = new String[theImagesArrayList.size()];

    for (int i = 0; i < theImagesArrayList.size(); i++) {
        for (File b : files) {
            if (theImagesArrayList.contains(b.getName())) {
                    count++;
                    if(count<=theImagesArrayList.size()){
                        file[i] = b.getPath();
                        System.out.println(file[i]);
                        /*Output from above print statement:

                         C:\Users\someone\Desktop\slideshow images\Snow Fall.jpg
                         C:\Users\someone\Desktop\slideshow images\Soft snow.jpg
                         C:\Users\someone\Desktop\slideshow images\Winter Wonderland.jpg
                         */

                }
            }
        }
    }
    System.out.println(" ");
    for(String c: file){
        System.out.println(c);
    }
    /*Output above loop:

      C:\Users\someone\Desktop\slideshow images\Winter Wonderland.jpg
      null
      null
     */
    }
  }
 }
}
/*ArrayList内容:
雪花飘落.jpg
Soft snow.jpg
冬季仙境.jpg
*/
public void getImages(阵列列表图像阵列列表){
File dir=新文件(“C:/Users/someone/Desktop/slideshow image”);
整数计数=0;
File[]files=dir.listFiles();
file=新字符串[theImagesArrayList.size()];
对于(int i=0;i如果(count那么
count++
就是问题所在。
文件[i]=b.getPath();
在第一次外部迭代中执行三次,在下一次迭代中不再执行。因此,所有三条路径都写入
文件[0]
。这就是
文件[1]
文件[2]
保持空值的原因

尝试在
System.out.println(文件[i]);
指令之后
中断;

在第一次迭代中,内部循环中变量“i”的值始终为0,因此赋值后“i”的值不会增加,因此总是用最新的值覆盖它

代替语句:file[i]=b.getPath()

您可以使用这样的语句:file[j++]=b.getPath();//decare并在之前初始化j=0