【问题标题】:Nested for-loop to check arraylist against another arraylist嵌套 for 循环以检查 arraylist 与另一个 arraylist
【发布时间】:2015-12-02 12:58:37
【问题描述】:

我有一个主数组列表(称为filteredArrayList,我想在其上导入另一个数组列表(称为importArrayList),过滤值并创建一个新的checkedArrayList,以获取真正唯一的然后追加checkedArrayList 终于交给主人了。

我的代码的问题是导入总是导入importArrayList 中的所有对象,而不是实际检查当前值。

ArrayList<Patient> checkedPatientList=new ArrayList<Patient>();
                mainloop:for(Patient imp:importPatientList){
                        for(Patient p: filteredPatientList){
                            if (imp.getpID()!=p.getpID()){
                                checkedPatientList.add(imp);
                                continue mainloop;


                            }
                        }
                    }

我尝试了很多事情但都失败了,欢迎任何关于如何做的想法。

【问题讨论】:

  • 能否请您提供 Patient 类?
  • 如果您想要唯一的值,Set 不是更有用吗?
  • 您必须进一步解释“独特”的含义。对您来说唯一是否意味着它不是同一个对象(不是相同的引用)或唯一意味着 2 个患者不包含相同的数据?
  • 您的 if / add 不正确:当只有一个 ID 不同时,它会添加。查看我的代码
  • 对不起,我应该提到处理了唯一性。 .getpID() 获取患者的唯一 ID,该 ID 始终是唯一的(它基于哈希码,我已经对其进行了测试)。

标签: java for-loop arraylist nested


【解决方案1】:

试试这个逻辑

for (String item : pinkList) {
    if (normalList.contains(item)) {
        duplicateList.add(item);
    } else {
        uniqueList.add(item);
    }
}

【讨论】:

    【解决方案2】:

    您的代码不正确:

    for(Patient p: filteredPatientList){
                            if (imp.getpID()!=p.getpID()){
                                checkedPatientList.add(imp);
    

    你必须在导入前检查所有的checkedPatientList。

    一套更简单,更快。

    假设

    public class Patient
    {
    public String getpID()
        {
        return "XX";
        }
    }
    
    
    
    ArrayList<Patient> importPatientList=new ArrayList<Patient>();
    
    ArrayList<Patient> checkedPatientList=new ArrayList<Patient>();
    
    
    // Do a Set with ID
    Set<String> unique_id=new HashSet<String>();
    
     for(Patient p: checkedPatientList)
         unique_id.add(p.getpID());
    
    
    // every patient in imported if not already checked
    for(Patient imp:importPatientList)
    if (!unique_id.contains(imp.getpID()))
               checkedPatientList.add(imp);
    

    【讨论】:

    • +1 是唯一一个花时间解释为什么现有代码不起作用的人。我不知道是谁给你打了分,但如果他们能解释一下原因就很礼貌了。
    【解决方案3】:

    我终于明白了!谢谢大家的回答,对我很有帮助。

    如果我学到了什么,那就是在嵌套的 for 循环中,在进入循环之前要小心你所做的事情!

    boolean isunique=true;
                        for(Patient imp: importPatientList){                                                                            
                            for(Patient p: filteredPatientList){
                                if (imp.getpID()==p.getpID()){
                                    isunique=false;
                                    break;
                                }
                                else{
                                    isunique=true;
                                }
                            }
                            if(isunique){
                                checkedPatientList.add(imp);
                            }
                        }
    

    【讨论】:

      【解决方案4】:

      试试这个-

      if (imp.getpID().equals(p.getpID())){
      ......
      }
      

      现在,我假设您已获得所需的患者列表,并且该列表的名称是患者。现在您需要做的是将列表分配给 Set 以使集合独一无二。

      Set<Patient> set = new HashSet<Patient>(patients);
      

      如果你可以只用套装移动,那没关系。但是,如果您再次需要列表,请执行以下操作-

      List<Patient> patients = new ArrayList<Patient>(set);
      

      现在列表包含所有唯一的患者。

      【讨论】:

        【解决方案5】:

        我认为最简单的方法是使用 Set。然后如果你真的需要,你可以将它的内容复制到新的 ArrayList。

        Set filteredSet = new HashSet<>();
        filteredSet.addAll(filteredPatientList);
        filteredSet.addAll(importPatientList);
        List checkedPatientList = new ArrayList<>(filteredSet);
        

        【讨论】:

          【解决方案6】:

          你给自己的答案在技术上是正确的,但如果你只是想用更少的代码行稍微整理一下,那么你可能更喜欢这样的东西:

          ArrayList<Patient> checkedPatientList=new ArrayList<Patient>();    
          
          for(Patient imp : importPatientList) {
              boolean patientIsUnique = true;
          
              for(Patient p : filteredPatientList) {
                  if (imp.getpID() == p.getpID()) {
                      patientIsUnique = false;
                  }                            
              }        
              if (patientIsUnique) {
                  checkedPatientList.add(imp);
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2013-10-20
            • 1970-01-01
            • 2014-06-10
            • 1970-01-01
            • 2022-01-10
            • 2014-01-12
            • 1970-01-01
            • 2017-06-18
            • 2021-01-03
            相关资源
            最近更新 更多