我有两个来自Parse Query的ArrayLists,我正在与一个增强的for循环进行比较。我想检查2列表中是否有匹配的ObjectId,如果是这样,我想从“allDropList”中删除该Object。所有其他组件都正常工作,我相信我的增强for循环语法的问题。这是我的代码,谢谢!注意:我已经尝试过“if”和“while”,但没有成功。
public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList , ArrayList <DropItem> allDropsList){
for(DropItem dropItemAll : allDropsList) {
for(DropItem dropItemRelation : hasRelationList) {
/*if*/ while(dropItemAll.getObjectId().equals(dropItemRelation.getObjectId())) {
allDropsList.remove(dropItemAll);
}
}
}
return allDropsList;
}
答案 0 :(得分:0)
问题是你无法迭代列表并使用for
循环同时删除某些内容。请查看here以获得进一步说明。
答案 1 :(得分:0)
以相反的顺序删除列表中的对象更安全:
public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList ,ArrayList <DropItem> allDropsList){
for(int i = allDropsList.size()-1; i >=0; i--) {
for(int j = hasRelationList.size()-1; j >=0; j--) {
if(allDropsList.get(i).getObjectId().equals(hasRelationList.get(j).getObjectId())) {
allDropsList.remove(i);
}
}
}
return allDropsList;
}
答案 2 :(得分:0)
或者你可以简单地这样做:
public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList , ArrayList <DropItem> allDropsList){
ArrayList<DropItem> temp = new ArrayList<DropItem>(); //place the objects you want inside this arraylist
for(DropItem dropItemAll : allDropsList) {
int counter = 0;
for(DropItem dropItemRelation : hasRelationList) {
if(dropItemAll.getId().equals(dropItemRelation.getId())) {
break;
}
if(counter == hasRelationList.size()-1){
temp.add(dropItemAll);
}
counter++;
}
}
return temp;
}
答案 3 :(得分:0)
如果您使用Iterator代替外部循环,则可以在迭代时从列表中删除项目,而无需借助其他&#39;。
import numpy as np
import matplotlib.pyplot as plt
from cartopy import crs
# a grid for the longitudes and latitudes
lats = np.linspace(-90, 90, 50)
longs = np.linspace(-180, 180, 50)
lats, longs = np.meshgrid(lats, longs)
# some data
data = lats[1:] ** 2 + longs[1:] ** 2
fig = plt.figure()
# create a new axes with a cartopy.crs projection instance
ax = fig.add_subplot(1, 1, 1, projection=crs.Mollweide())
# plot the date
ax.pcolormesh(
longs, lats, data,
cmap='hot',
transform=crs.PlateCarree(), # this means that x, y are given as longitude and latitude in degrees
)
fig.tight_layout()
fig.savefig('cartopy.png', dpi=300)
答案 4 :(得分:0)
这里是使用Iterator的更正答案。 这是一个编译和运行的完整示例:
import java.util.ArrayList;
import java.util.Iterator;
public class DropTest {
class DropItem {
private String objectId;
public String getObjectId() {
return objectId;
}
public DropItem(String id) {
this.objectId = id;
}
}
public static void main(String[] args) {
new DropTest().test();
}
public void test() {
ArrayList<DropItem> allDropsList = new ArrayList<>();
allDropsList.add(new DropItem("one"));
allDropsList.add(new DropItem("two"));
allDropsList.add(new DropItem("three"));
allDropsList.add(new DropItem("four"));
allDropsList.add(new DropItem("five"));
ArrayList<DropItem> hasReleationList = new ArrayList<>();
hasReleationList.add(new DropItem("three"));
hasReleationList.add(new DropItem("five"));
System.out.println("before:");
for(DropItem dropItem : allDropsList) {
System.out.println(dropItem.getObjectId());
}
ArrayList<DropItem> result = filterDrops(hasReleationList, allDropsList);
System.out.println("\n\nafter:");
for(DropItem dropItem : result) {
System.out.println(dropItem.getObjectId());
}
}
public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList, ArrayList <DropItem> allDropsList) {
Iterator<DropItem> allDropsIterator = allDropsList.iterator();
while(allDropsIterator.hasNext()) {
DropItem dropItemAll = allDropsIterator.next();
for(DropItem dropItemRelation : hasRelationList) {
if(dropItemAll.getObjectId().equals(dropItemRelation.getObjectId())){
allDropsIterator.remove();
}
}
}
return allDropsList;
}
}