我怎样才能从ArrayList中重复值? [英] How can I get duplicated values from ArrayList?
问题描述
还有就是我的code:
There is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class CompareFile {
CompareFile() {}
boolean check = false;
int count = 0;
/*
* this method return a File type variable
* variable path - this parameter takes a path in specified root
* also it's a private method and he should be called with help by class object
*/
public File find(File path) throws FileNotFoundException {
PrintWriter writer = new PrintWriter("D:/Photos/name.txt");
ArrayList<String> buffer = new ArrayList<String>();
/*
* let's create array of files
* where must be all found files
* We use File[], and named 'files'
* variable files takes on list of found files
* with help by listFiles method, that
* return list of files and directories
*/
File[] files = path.listFiles();
//System.out.println(files.toString());
try {
/*
* we'll ought use the for each loop
* in this loop we'll create File type variable 'file'
* then we'll do iterating for each expression from variable files
*/
for (File file : files) {
//print all found files and directories
//if file or directory exists
if (file.isDirectory()) {
//recursively return file and directory
find(file);
}
else {
buffer.add(file.getName());
System.out.println(file);
}
}
}
catch (Exception e) {
System.out.print("Error");
}
finally{
if ( writer != null )
writer.close();
}
/*
Iterator<String> i = buffer.iterator();
while(i.hasNext()) {
System.out.println(i.next());
}*/
return path;
}
public static void main(String[] args) throws FileNotFoundException {
File mainFile = new File("D:/Photos/");
CompareFile main = new CompareFile();
main.find(mainFile);
}
}
如果我使用排序,排序,结果不好,因为第一排从目录照片之后,
从目录目录第二排照片/子让我们来看看
屏幕:我想你懂的)
If I use sort, after sorting, the result bad, because first row from dir "Photos", and second row from directory in directory "Photos/sub" Let's look at the screen: I think you understand)
http://s10.postimg.org/7hcw83z9x/image.png
推荐答案
您将需要跟踪你在树枝上的,更改找到
方法采取的路径:
You'll need to keep track of where you are in the tree, change the find
method to take a path:
公开文件中找到(文件路径,路径字符串=)抛出FileNotFoundException异常
在递归调用find方法:查找(文件,路径+ file.getName()+/)
When you call the find method recursively:
find(file, path + file.getName() + "/")
然后,当您添加到列表中,使用 buffer.add(路径+ file.getName());
Then when you add to the list, use
buffer.add(path + file.getName());
这篇关于我怎样才能从ArrayList中重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!