从两个阵列中查找公共文件

时间:2022-06-23 12:47:46

I am trying to find common name files from two arrays. I have saved file names of two different folders in two different arrays.

我试图从两个数组中找到常用的名称文件。我已经在两个不同的数组中保存了两个不同文件夹的文件名。

Now i am creating a common file array which will have files which have common name.

现在我正在创建一个公共文件数组,其中包含具有通用名称的文件。

filenames 1 : Array contaning names of file in folder 1.

文件名1:包含文件夹1中文件名的数组。

filename 2 : Array contaning names of file in folder 2.

filename 2:包含文件夹2中文件名的数组。

import java.io.File;
import java.util.*;

public class ListFiles1 
{

    public static void main(String[] args) 
    {

        String path1 = "C:\\"; 

        String path2 = "D:\\"; 

        File folder1 = new File(path1);
        File folder2 = new File(path2);

        String[] f1=folder1.list();

        File[] listOfFiles1 = folder1.listFiles(); 
        File[] listOfFiles2 = folder2.listFiles(); 

        ArrayList<String> fileNames1 = new ArrayList<>();
        ArrayList<String> fileNames2 = new ArrayList<>();

        for (int i = 0; i < listOfFiles1.length; i++) 
        {

            if (listOfFiles1[i].isFile()) 
            {
                fileNames1.add(listOfFiles1[i].getName());
                System.out.println(f1[i] + " is a file");
            }

        }

        for (int j = 0; j < listOfFiles2.length; j++) 
        {

            if (listOfFiles2[j].isFile()) 
            {
                fileNames2.add(listOfFiles2[j].getName());
            }
        }

        ArrayList<String> commonfiles = new ArrayList<>();
        for (int i = 0; i < listOfFiles1.length; i++)
        {
        for (int j = 0; i < listOfFiles2.length; j++) 
            {
            String tempfilename1;
            String tempfilename2;
            tempfilename1=fileNames1[i];
            tempfilename2 = fileNames2[j];
            if(tempfilename1.equals(tempfilename2))
                {
                commonfiles.add(tempfilename1);
                }
            }
        }                   
    }
}

I wrote this code but the compiler gives this error:

我写了这段代码,但编译器给出了这个错误:

   Main.java:52: error: array required, but ArrayList<String> found
            tempfilename1=fileNames1[i];
                                    ^
Main.java:53: error: array required, but ArrayList<String> found
            tempfilename2 = fileNames2[j];
                                      ^

P.S : I am a Newbie...

P.S:我是新手......

2 个解决方案

#1


5  

If filenames1 is an array you cannot use ArrayList method get(int i).

如果filenames1是一个数组,则不能使用ArrayList方法get(int i)。

You need to access the array elements by using arrayName[elementIndex].

您需要使用arrayName [elementIndex]访问数组元素。

In your case:

在你的情况下:

tempfilename1=filenames1[i];
tempfilename2=fileNames2[j];

#2


0  

You could do the following to get the common set of strings between both arrays:

您可以执行以下操作以获取两个阵列之间的公共字符串集:

    String[] listOfFiles1 = {"file1", "file2", "file3"};
    String[] listOfFiles2 = {"file2", "file3", "file5"};

    HashSet<String> common = new HashSet<String>(Arrays.asList(listOfFiles1));
    common.retainAll(new HashSet<String>(Arrays.asList(listOfFiles2)));

    System.out.println(common); // -> output: file2, file3

You don't need to convert them to Sets though, but this would remove the risk of duplicate keys. If you do want to show duplicates you could simplify it a bit:

您不需要将它们转换为集合,但这将消除重复键的风险。如果你想显示重复项,你可以简化一下:

    List<String> common = new ArrayList<String>(Arrays.asList(listOfFiles1));
    list1.retainAll(Arrays.asList(listOfFiles2));

#1


5  

If filenames1 is an array you cannot use ArrayList method get(int i).

如果filenames1是一个数组,则不能使用ArrayList方法get(int i)。

You need to access the array elements by using arrayName[elementIndex].

您需要使用arrayName [elementIndex]访问数组元素。

In your case:

在你的情况下:

tempfilename1=filenames1[i];
tempfilename2=fileNames2[j];

#2


0  

You could do the following to get the common set of strings between both arrays:

您可以执行以下操作以获取两个阵列之间的公共字符串集:

    String[] listOfFiles1 = {"file1", "file2", "file3"};
    String[] listOfFiles2 = {"file2", "file3", "file5"};

    HashSet<String> common = new HashSet<String>(Arrays.asList(listOfFiles1));
    common.retainAll(new HashSet<String>(Arrays.asList(listOfFiles2)));

    System.out.println(common); // -> output: file2, file3

You don't need to convert them to Sets though, but this would remove the risk of duplicate keys. If you do want to show duplicates you could simplify it a bit:

您不需要将它们转换为集合,但这将消除重复键的风险。如果你想显示重复项,你可以简化一下:

    List<String> common = new ArrayList<String>(Arrays.asList(listOfFiles1));
    list1.retainAll(Arrays.asList(listOfFiles2));