如何从 ArrayList 中获取重复值? [英] How can I get duplicated values from ArrayList?
问题描述
这是我的代码:
import java.io.File;导入 java.io.FileNotFoundException;导入 java.io.PrintWriter;导入 java.util.ArrayList;导入 java.util.Collections;导入 java.util.Iterator;公共类比较文件 {比较文件(){}布尔检查 = 假;整数计数 = 0;/** 此方法返回一个文件类型变量* 变量路径 - 此参数采用指定根目录中的路径* 它也是一个私有方法,应该在类对象的帮助下调用他*/公共文件查找(文件路径)抛出 FileNotFoundException {PrintWriter writer = new PrintWriter("D:/Photos/name.txt");ArrayList缓冲区 = 新的 ArrayList();/** 让我们创建文件数组* 必须在哪里找到所有文件* 我们使用 File[],并命名为 'files'* 变量文件采用找到的文件列表* 在 listFiles 方法的帮助下,* 返回文件和目录列表*/File[] 文件 = path.listFiles();//System.out.println(files.toString());尝试 {/** 我们应该使用 for each 循环* 在这个循环中,我们将创建文件类型变量 'file'* 然后我们将对变量文件中的每个表达式进行迭代*/对于(文件文件:文件){//打印所有找到的文件和目录//如果文件或目录存在如果(文件.isDirectory()){//递归返回文件和目录查找(文件);}别的 {buffer.add(file.getName());System.out.println(文件);}}}捕获(异常 e){System.out.print("错误");}最后{如果(作家!= null)writer.close();}/*迭代器<字符串>i = buffer.iterator();while(i.hasNext()) {System.out.println(i.next());}*/返回路径;}public static void main(String[] args) 抛出 FileNotFoundException {File mainFile = new File("D:/Photos/");CompareFile main = new CompareFile();main.find(mainFile);}}
如果我使用排序,排序后,结果不好,因为来自目录照片"的第一行,和目录Photos/sub"中目录的第二行让我们看看屏幕:我想你明白)
http://s10.postimg.org/7hcw83z9x/image.png
您需要跟踪您在树中的位置,将 find
方法更改为采用路径:>
public File find(File path, string path="") 抛出 FileNotFoundException
递归调用find方法时:find(file, path + file.getName() + "/")
然后当你添加到列表中时,使用buffer.add(path + file.getName());
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:
public File find(File path, string path="") throws FileNotFoundException
When you call the find method recursively:
find(file, path + file.getName() + "/")
Then when you add to the list, use
buffer.add(path + file.getName());
这篇关于如何从 ArrayList 中获取重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!