【问题标题】:Comparing two lists to calculate the difference(s) - Android / Java比较两个列表以计算差异 - Android / Java
【发布时间】:2014-11-20 06:24:39
【问题描述】:

我有两个列表 - 一个返回所有文件夹的列表,另一个返回最近文件夹的列表。我想做的是将每个列表相互比较 - 如果找到匹配项:显示敬酒。

我目前的代码如下:

    boolean currentFolderFound = false;
            if (mAllFolderListCursor != null) {
                final String folderName = mSelectedFolderUri.toString();
                if (mAllFolderListCursor.moveToFirst()) {
                    do {
                        final Folder f = mAllFolderListCursor.getModel();
                        if (!isFolderTypeExcluded(f)) {
                            if (f.folderUri.equals(mRecentFolders)) {
                                Toast.makeText(getApplicationContext(), "MATCH FOUND",
                                           Toast.LENGTH_LONG).show();
                            }
                        }
                    } while (!currentFolderFound && mAllFolderListCursor.moveToNext());
                }
}

我相信我已经编写了以下代码:如果 (f.folderUri.equals(mRecentFolders)) { 错误 - 我想知道如何改进它以提供我正在寻找的结果。

提前致谢,

克里斯托弗

【问题讨论】:

  • 你会想要像 if(mRecentFolders.contains(f.folderUri)) 这样的东西,这可能会也可能不会,这取决于你在那里比较的东西类型。

标签: java android android-cursor


【解决方案1】:

equals 如果对象实际上相等,则返回 true,但在您的情况下它们不是。您应该使用 compareTo 方法进行简单的字符串比较。

String f1 = folder1.folderUri.toString();
String f2 = folder2.folderUri.toString();
if (f1.compareTo(f2) == 0) { // Do what needs to be done}

【讨论】:

  • 其实是一个RecentFolderList: private final RecentFolderList mRecentFolders;
  • 因此,您应该针对 mAllFolderList 中的每个文件夹测试此列表中的每个文件夹。如果我明白你想做什么。我的回答只是关于文件夹比较...
猜你喜欢
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 2020-07-25
  • 2014-02-26
  • 2013-04-25
  • 1970-01-01
相关资源
最近更新 更多