从列表中删除名称字段与给定参数匹配的所有 Person 实例
练习要求从列表集合中删除与给定名称参数匹配的所有人员。该解决方案必须使用 for-each 循环。
下面介绍的解决方案不会向我抛出任何编译时错误,但它无法通过为验证解决方案的正确性而运行的单元测试之一。
这是我到目前为止所尝试过的:
public ArrayList<Person> people;
public void remove(String name){
for(Person i : people){
if (people.contains(name)){
people.remove(i);
}
}
}
An exercise demands that all people matching a given name parameter be removed from a List collection. The solution must use a for-each loop.
The solution presented below, does not throw any compile-time errors at me, but but it fails to pass one of the Unit tests that are run to verify the correctness of the solution.
Here is what I have tried so far:
public ArrayList<Person> people;
public void remove(String name){
for(Person i : people){
if (people.contains(name)){
people.remove(i);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
哪个测试没有通过?
但让我们看一下代码:
您得到一个参数“name”。然后您迭代人员列表。当前人是i。在每个步骤中,您都会询问列表是否包含名称(这是一个字符串)。嗯。
contains 询问整个列表,它是否包含某些东西 - 这个东西应该与列表中元素的类型匹配,不是吗?
但是在迭代时,您可以询问每个人(i),该人的名字是否是“name”。
迭代同时删除是另一个需要注意的问题。
由于有很多谣言,而且很多人似乎都在监督这个问题,所以我有一个不同的解决方案,而不是使用迭代器:收集要删除的候选者,最后将整个黑名单作为一组减去:
Which test isn't passed?
But let's look at the code:
You get a param 'name'. Then you iterate over a list of Persons. The current person is i. In each step, you ask the list, whether it contains the name (which is a String). Hm.
contains asks the whole list, whether it contains something - this something should match the typ of the elements in the list, shouldn't it?
But while iterating, you could ask each Person (i), whether the persons name is 'name'.
Iterating an meanwhile removing is another issue, to take care of.
Since there is much rumor, and a lot of people seemed to oversee the problem, I have a different solution, than using an interator: Collect the candidates for removing, and substract the whole blacklist as a bunch in the end:
当然,这需要您的类有一个名为
getName()
的方法。或者您可以覆盖 equals 方法,但要注意此答案的评论中所述的缺点。http:// /download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html#contains(java.lang.Object)
This will require that you person class has a method named
getName()
of course. Or you could override equals method, however beware of drawbacks as stated in the comments of this answer.http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html#contains(java.lang.Object)
编辑:
我的第一个答案(在线下)实际上是错误,就像迄今为止发布的所有其他答案一样。我做了一个小测试(如下)...我们都在 foreach 循环下面的迭代器中导致了 ConcurrentModificationException 。现在有一个问题要告诉你!叹。
输出
老兄,
这个答案是错误的,请忽略它!!!
我建议您使用 http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html#remove% 28java.lang.Object%29 代替。
像这样的东西:
...假设 Person 有一个 getName 方法,它返回一个字符串。
干杯。基思.
EDIT:
My first answer (below the line) is actually WRONG, just like all the others posted so far. I did-up a little test (below)... We're ALL causing a ConcurrentModificationException in the iterator which underlies the foreach loop. Now there's a gotcha for ya! Sigh.
Output
Dude,
THIS ANSWER IS WRONG, PLEASE IGNORE IT!!!
I suggest you use http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html#remove%28java.lang.Object%29 instead.
Something like:
... presuming the Person has a
getName
mothod, which returns a string.Cheers. Keith.
如果要从列表中删除,请不要使用 for-each 循环:
(来自文档)
使用 标准迭代器 及其
remove
方法。Don't use for-each loops if you about to remove from your list:
(from documentation)
Use standard iterator and its
remove
method instead.我不知道如何将名称存储在 Person 类中,但也许你可以这样做。请记住,我将第 i 个元素存储在人员数组列表中,因此每次迭代您都需要检查当前人员的姓名以检查是否需要删除它
I dont know how do you store the name on the Person class but maybe you could do something like this. Remember that i stores ith element on the people arraylist so each iteration you need to check for the name of the current person to check if you need to remove it