【问题标题】:Adding class/object to Array list if not a duplicate value present如果不存在重复值,则将类/对象添加到数组列表
【发布时间】:2015-02-09 21:12:50
【问题描述】:

我很难理解我的逻辑发生了什么。我的目标是如果当前正在制作的数组列表中不存在对象上的字符串,则将对象添加到数组列表中。

所以在下面解释我的代码/我的意图..

当我添加到 rowItems 时,检查已经添加到 rowItems 的 versionId,如果是,则附加当前对象的名称。

这是我目前所拥有的:

      rowItems = new ArrayList<>();

            for (int i = 0; i < SearchResultsHolder.results.size(); i++) {
                SearchRowItem item = new SearchRowItem(SearchResultsHolder.results.get(i).get("LAST_NAME").toString() + ", " + SearchResultsHolder.results.get(i).get("FIRST_NAME").toString(),
                        SearchResultsHolder.results.get(i).get("BUSINESS_NAME").toString(),
                        SearchResultsHolder.results.get(i).get("LOB").toString(),
                        SearchResultsHolder.results.get(i).get("ID_NUMBER").toString(),
                        cleanCancelled(SearchResultsHolder.results.get(i).get("STATUS").toString()),
                        SearchResultsHolder.results.get(i).get("EFF_DATE").toString(),
                        SearchResultsHolder.results.get(i).get("EXP_DATE").toString(),
                        SearchResultsHolder.results.get(i).get("VERSION_ID").toString());

                if (i == 0){
                    rowItems.add(item);//add first object by default

                }

                else {
                    for (int p = 0; p < rowItems.size(); p++) {
                        // if the version id is equal to any of the others already in the rowItems array appent the name to the object already in the array
                        if (item.getVersionId().toString().equals(rowItems.get(p).getVersionId().toString()))
                        {
                            item.setName(item.getName().toString() + "\n" + rowItems.get(p).getName().toString());
                          break;
                        } else {
                            rowItems.add(item);

                        }
                    }
                }

            }
            System.out.println("row item" + rowItems.toString());

SearchResults 持有者的布局如下:

[
    {
        VERSION_ID=50,
        STATUS=Active,
        ID_NUMBER=1234,
        FIRST_NAME=JOHN,
        LAST_NAME=DOE       
    },
        {
        VERSION_ID=50,
        STATUS=Active,
        ID_NUMBER=1234,
        FIRST_NAME=JANE,
        LAST_NAME=DOE       
    },
        {
        VERSION_ID=100,
        STATUS=Dead,
        ID_NUMBER=1234,
        FIRST_NAME=JOHN,
        LAST_NAME=DOE       
    },
    {
        VERSION_ID=100,
        STATUS=Dead,
        ID_NUMBER=1234,
        FIRST_NAME=JANE,
        LAST_NAME=DOE       
    },
]

以及我试图用我班级中的对象实现的目标:(rowitems arraylist)

[
    {
        VERSION_ID=50,
        STATUS=Active,
        ID_NUMBER=1234,
        NAME=JANE,DOE
             JOHN,DOE      
    },
        {
        VERSION_ID=100,
        STATUS=Dead,
        ID_NUMBER=1234,
        NAME=JANE,DOE
             JOHN,DOE
    },

]

我得到了什么:

[
    {
        VERSION_ID=50,
        STATUS=Dead,
        ID_NUMBER=1234,
        NAME=JOHN,DOE



    },
        {
        VERSION_ID=100,
        STATUS=Active,
        ID_NUMBER=1234,
        NAME=JOHN,DOE

    }


]

【问题讨论】:

  • 当你运行它时实际发生了什么?
  • @DavidConrad 添加了我从中得到的信息
  • 您是否尝试过使用调试器进行单步调试?

标签: java android arraylist


【解决方案1】:

你的这个条件不正确。

if (item.getVersionId().toString().equals(rowItems.get(p).getVersionId().toString()))
                {
                    item.setName(item.getName().toString() + "\n" + rowItems.get(p).getName().toString());
                } else {
                    rowItems.add(item);

                }

这意味着现在当不等于字符串时,您将其添加到arraylist。这是不正确的。您必须检查 rowItems 中的所有值是否不等于 到项目字符串。像这样的想法:

 boolean foundDup = false;
 for (int p = 0; p < rowItems.size(); p++) {

                // if the version id is equal to any of the others already in the rowItems array appent the name to the object already in the array
                if (item.getVersionId().toString().equals(rowItems.get(p).getVersionId().toString()))
                {
                    rowItems.get(p).setName(item.getName().toString() + "\n<ToSeeEndOfLine>" + rowItems.get(p).getName().toString()); // EDIT: this line is changed
                    foundDup = true;
                    break;
                } 
            }
     if (!foundDup)
            rowItems.add(item);

【讨论】:

  • 为了提高效率,还应该在找到后添加一个break。
  • @kolisko 我添加了中断,它确实有助于更接近我正在寻找但仍然不存在的内容,请查看在 for 循环中添加了中断的新输出
  • 你现在使用的当前输出和当前代码是什么?
  • 啊,我看到你编辑了它。您带有“break”的新代码仍然不正确。检查我的示例,在 for 循环之外添加了“foundDup”变量和“if”条件。
  • @kolisko 好的,效果好多了!仍然没有附加名称,必须进行一些测试才能了解原因。查看我的更新输出
猜你喜欢
  • 2021-08-30
  • 1970-01-01
  • 2021-12-07
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 2018-08-09
相关资源
最近更新 更多