Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                                                                                            
                
        
我有两个列表,即addListdeleteList

addList中的元素是AddEntity类型,具有两个字段

- id
- parentId


AddEntity类如下

public class AddEntity{
    int id;
    int parentId;

    //getters and setters here
}


并且deleteList中的实体属于DeleteEntity类型,其中只有一个字段

- deleteId


DeleteEntity类如下

public class DeleteList{
    int deleteId;
    //gettter and setter for deleteId goes here
}


现在我有两个清单
List<AddEntity> addListList<DeleteEntity> deleteList

例如addList的内容是

id          parentId

2001          3
2002          2001
2003          2001
2004          2002
2005          2003
2006          4
2007          2006


deleteList的内容是

deleteId

2001
3
2007


现在,我想(递归)从addListid/parentId中元素的deleteId匹配的deleteList中删除​​所有实体及其子级。
我也想只保留与deleteList中的任何ID不匹配的addList中的实体。

例如,在这种情况下,在处理了上面两个列表之后,addList的内容应为

id      parentId

2006    4


deleteList现在将包含

deleteId

3


我的逻辑正确,但是在实现部分遇到了一些问题。我正在用JAVA来做。希望在这里找到一些解决方案。
谢谢!

编辑

-(因为有些人对这个问题不高兴)

我的方法

实际上,逻辑很简单。但是有点混乱。

Step1: For each elements in the deleteList{
        For each elements in the addList{
          a) Match deleteId with id of each element in addList.
             if(deleteId==id){
               mark current element from deleteList for deletion
               loop:    check if any other element in addList has parentId==id.
                        if YES mark it(addList element) for delete
                        take the id of the marked element and goto "loop"

        }
      }
Step2: Delete All Marked Elements!!


最初,我尝试使用foreach并从列表中删除元素,而不是将其标记为删除。这导致ConcurrentModificationException。然后,我使用Iterator遍历列表。那是我被困住并摆姿势的地方。

最佳答案

看一下这个。在这里,我将发布整个代码,但是我仅针对几种情况进行了测试。欢迎所有评论。

AddList类

public class AddList {
private int id;
private int parentId;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getParentId() {
    return parentId;
}

public void setParentId(int parentId) {
    this.parentId = parentId;
}
}


DeleteList类

public class DeleteList {
private  int deleteId;

public int getDeleteId() {
    return deleteId;
}

public void setDeleteId(int deleteId) {
    this.deleteId = deleteId;
}
}


这是主班

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) throws ParseException {
    List<AddList> addList = new ArrayList<AddList>();
    List<DeleteList> delList = new ArrayList<DeleteList>();
    AddList addL1 = new AddList();
    AddList addL2 = new AddList();
    AddList addL3 = new AddList();
    AddList addL4 = new AddList();
    AddList addL5 = new AddList();
    AddList addL6 = new AddList();
    AddList addL7 = new AddList();

    DeleteList delL1 = new DeleteList();
    DeleteList delL2 = new DeleteList();
    DeleteList delL3 = new DeleteList();

    addL1.setId(2001);
    addL1.setParentId(3);
    addL2.setId(2002);
    addL2.setParentId(2001);
    addL3.setId(2003);
    addL3.setParentId(2001);
    addL4.setId(2004);
    addL4.setParentId(2002);
    addL5.setId(2005);
    addL5.setParentId(2003);
    addL6.setId(2006);
    addL6.setParentId(4);
    addL7.setId(2007);
    addL7.setParentId(2006);

    delL1.setDeleteId(2001);
    delL2.setDeleteId(3);
    delL3.setDeleteId(2007);

    addList.add(addL1);
    addList.add(addL2);
    addList.add(addL3);
    addList.add(addL4);
    addList.add(addL5);
    addList.add(addL6);
    addList.add(addL7);

    delList.add(delL1);
    delList.add(delL2);
    delList.add(delL3);

    removeElements(addList, delList);
}

public static void removeElements(List<AddList> add, List<DeleteList> del) {
    boolean status = true;
    int[] temp = new int[del.size()];
    int[] child = new int[add.size()];
    int i = 0;
    while (status) {
        for (int j = 0; j < add.size(); j++) {
            if (del.get(i).getDeleteId() == add.get(j).getId()) {
                add.remove(j);
                temp[i] = del.get(i).getDeleteId();
                j = -1;
            }
        }
        i++;
        if (i == del.size()) {
            status = false;
        }
    }
    i = 0;
    int k = 0;
    boolean newStatus = true;
    while (newStatus) {
        for (int j = 0; j < add.size(); j++) {
            if (temp[i] == add.get(j).getParentId()) {
                child[k] = add.get(j).getId();
                add.remove(j);
                k++;
                j = -1;
            }
        }
        i++;
        if (i == del.size()) {
            newStatus = false;
        }
    }
    i = 0;
    boolean con = true;
    while (con) {
        for (int j = 0; j < del.size(); j++) {
            if (temp[i] == del.get(j).getDeleteId()) {
                del.remove(j);
                j = -1;
            }
        }
        i++;
        if (i == temp.length) {
            con = false;
        }
    }

    i = 0;
    boolean cons = true;
    while (cons) {
        for (int j = 0; j < add.size(); j++) {
            if (child[i] == add.get(j).getParentId()) {
                add.remove(j);
                j = -1;
            }
        }
        i++;
        if (i == child.length) {
            cons = false;
        }
    }
}

}

关于java - 比较和删除列表中的元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17626946/

10-09 05:05