1

addList我有两个列表,即deleteList

中的元素是具有两个字段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

现在我addList想删除id/parentIddeleteId. 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 迭代列表。这就是我被卡住并提出这个问题的地方。

4

1 回答 1

1

看一下这个。在这里,我发布了我的整个代码,但我只测试了几个场景。欢迎所有评论。

添加列表类

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;
}
}

删除列表类

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;
        }
    }
}

}
于 2013-07-13T07:31:18.577 回答