它从未被用过
It is never used else
我正在用 Grails 做一些工作,我永远不能让程序通过 else 部分。我的代码如下
while (pois.hasNext()){
def poi=pois.next()
def site = Site.get(poi.site)
if (!sites.contains(site.id)){
sites.add([id:site.id.toString(),name:site.name])
}else{
println("It has been previously added")
}
}
我检查了我的数据库,它应该会通过
From doc :
Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).
您的 collection 包含 对 siteId:siteName
,并且您正在尝试检查它是否包含 siteId
.
你应该再有一个 collection,它只包含 siteId
。
或者,您将在 Map
上更改 sites
,您的代码将如下所示:
if (!sites.containsKey(site.id)){
sites.put(site.id.toString(), site.name)
}else{
println("It has been previously added")
}
或者,您应该检查 collection 中的所有元素。
def isExist = false;
for (def element: sites) {
if(elemtn.id == site.id) {
isExist = true;
}
}
if (!isExist){
sites.add([id:site.id.toString(),name:site.name])
}else{
println("It has been previously added")
}
我正在用 Grails 做一些工作,我永远不能让程序通过 else 部分。我的代码如下
while (pois.hasNext()){
def poi=pois.next()
def site = Site.get(poi.site)
if (!sites.contains(site.id)){
sites.add([id:site.id.toString(),name:site.name])
}else{
println("It has been previously added")
}
}
我检查了我的数据库,它应该会通过
From doc :
Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).
您的 collection 包含 对 siteId:siteName
,并且您正在尝试检查它是否包含 siteId
.
你应该再有一个 collection,它只包含 siteId
。
或者,您将在 Map
上更改 sites
,您的代码将如下所示:
if (!sites.containsKey(site.id)){
sites.put(site.id.toString(), site.name)
}else{
println("It has been previously added")
}
或者,您应该检查 collection 中的所有元素。
def isExist = false;
for (def element: sites) {
if(elemtn.id == site.id) {
isExist = true;
}
}
if (!isExist){
sites.add([id:site.id.toString(),name:site.name])
}else{
println("It has been previously added")
}