3

如何比较两个数组列表?

我有一个 sdCoverList 数组和 thumbnailList 数组。

02-04 11:05:05.210: I/System.out(4035): HASHED THUMBNAIL[-2122410790, 2043473787, 1914391068, 1785308349, 1656225630, 1527142911, 1398060192, 1268977473, 1139894754, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549]
02-04 11:05:05.210: I/System.out(4035): SD COVER LIST[-2122410790, 2043473787, 1914391068, 1785308349, 1268977473, 1656225630, 1527142911, 1139894754, 1398060192, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549, 717409028]

在我的 sd 封面列表中,我有一个额外的值要删除,但首先我必须先与 thumbnailList 数组进行比较。如果两者具有相同的元素,则不要做任何事情,但如果有多余的,请将其从 SD 卡中删除。

我的数组列表

对于 sdCoverList:

// CHECK DIRECTORY BEFORE DELETING THEM
            File strPath = new File(Environment.getExternalStorageDirectory() + folderName+"/Covers");
            File yourDir = new File(strPath, "");
            if(yourDir.length()!=0)
            for (File f : yourDir.listFiles()) {
                if (f.isFile())
                {
                    String name = f.getName();
                    sdCoverList.add(name);
                    //System.out.println(sdCoverList);
                }

对于散列缩略图

for (int a=0;a<=thumbnailList.size()-1;a++)
        {
            String hashed = String.valueOf(thumbnailList.get(a).hashCode());
            Log.d("hashed",hashed);
            hashedThumbnail.add(hashed);
        }
4

1 回答 1

9
List<String> sdCoverList = new ArrayList<String>();
sdCoverList.add("-2122410790");
sdCoverList.add("2043473787");
sdCoverList.add("717409028");

List<String> hashedThumbnail = new ArrayList<String>();
hashedThumbnail.add("-2122410790");
hashedThumbnail.add("2043473787");

System.out.println("sdCover List: " + sdCoverList);
System.out.println("hashedThumbnail List: " + hashedThumbnail);


List<String> temp = new ArrayList<String>();
temp.addAll(sdCoverList);
temp.removeAll(hashedThumbnail);

System.out.println("temp List: " + temp);

sdCoverList.removeAll(temp);

System.out.println("sdCover List: " + sdCoverList);

输出将是

sdCover List: [-2122410790, 2043473787, 717409028]
hashedThumbnail List: [-2122410790, 2043473787]
temp List: [717409028]
sdCover List: [-2122410790, 2043473787]
于 2013-02-04T03:39:13.193 回答