gpt4 book ai didi

java - 比较并删除列表中的元素

转载 作者:太空宇宙 更新时间:2023-11-04 07:31:29 25 4
gpt4 key购买 nike

我有两个列表,即 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 中的那些实体与 addList 中的任何 id 都不匹配.

例如,在本例中,处理以上两个列表后,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 。然后我使用迭代器来迭代列表。这就是我陷入困境并提出这个问题的地方。

最佳答案

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

添加列表类

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

}

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com