gpt4 book ai didi

java - 将值与数组列表内的值进行比较

转载 作者:行者123 更新时间:2023-12-02 13:11:49 25 4
gpt4 key购买 nike

我想比较数组列表中 FileVisitResult VisitFile 捕获的文件路径。进行此比较的原因是为了防止在数组列表中存储重复的文件路径,因为当条件为 true 时,FileVisitResult VisitFile 将继续循环。我想确保所有文件都刚刚被该方法访问过一次。所以每次FileVisitResult方法都会捕获文件路径并存储到arraylist中,当下一轮方法执行时,检测到的任何新文件路径都会与arraylist中的文件路径进行比较,但它仍然是在网上搜索了一些信息后未能做到这一点,Thread.sleep()用于停止程序以了解我的源代码流程

public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException 
{

String f;
File file1;
Path tempPath1,tempPath2;
if(counter == 0) //first round for the execution
{
counter++;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("The abs path of the file is"+file);
y2j.add(file); //add path to arraylist

f = file.toString();
file1 = new File(f);
if ( file1.exists()) //checking the file process whether still consume by any other process
{
RandomAccessFile statusCheck = null;
try
{
statusCheck = new RandomAccessFile(file1,"r");
if(statusCheck != null)
{

System.out.println("Not hodling by any process,clear to send for scanning");
statusCheck.close();
ambrose(); //do something in this method
}
}
catch(Exception e)
{
System.out.println("In processing : " + e.getMessage());

}
}
}
else if(counter > 0) //after first round of execution
{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("larger than 0");
System.out.println("The abs path of the file is"+file);
for(int i = 0 ; i<y2j.size()-1;i--)
{
System.out.println("In the for loop" +y2j);
for(int k = i+1 ; k<y2j.size();k++)
{
if(file == y2j.get(k))
{
System.out.println("they are same");
}
else if(file != y2j.get(k))
{
y2j.add(file); //add path to arraylist
ambrose(); //do something in this method

}
}
}

}
return FileVisitResult.CONTINUE;
}


The abs path of the file isC:\REST API\source\abc\abc.xlsx
Not hodling by any process,clear to send for scanning
Dirty deed here
larger than 0
The abs path of the file isC:\REST API\source\abc\def.txt
larger than 0
The abs path of the file isC:\REST API\source\abc\abc.xlsx
larger than 0
The abs path of the file isC:\REST API\source\abc\def.txt

看起来好像没有进入for循环,我花了一些时间,但不知道我的问题出在哪里。

最佳答案

使用 equals 方法代替 file == y2j.get(i) 。另外,您可以使用 contains 方法,而不是遍历整个列表,如果列表包含元素,该方法将返回 true。

java中的

== 通过比较引用来比较是否是完全相同的对象,但它不会区分看起来相同但不相同的对象,例如: new String( "n") == new String("n") 将返回 false。

关于java - 将值与数组列表内的值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43929058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com