【发布时间】:2018-11-17 04:35:45
【问题描述】:
我正在尝试搜索图像文件夹以检查每个图像是否 文件夹中的路径包含来自我的 ArrayList 的字符串元素。如果 文件路径包含字符串元素,我想将它添加到一个新的 字符串数组。这就是我到目前为止所拥有的。匹配的图像路径 数组列表中的字符串在 for 循环中正确打印出来,但是 除此之外,它会打印 null。
/*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
*/
}
}
}
}
【问题讨论】:
标签: java arrays arraylist directory