Say I have a method I call mutiple times (say 10 adverage per application run).
假设我有一个多次调用的方法(比如每个应用程序运行10个adverage)。
I iterate through a list, find an element with the proper ID and then pass that element as a parameter for another method.
我遍历一个列表,找到一个具有正确ID的元素,然后将该元素作为另一个方法的参数传递。
Does it make any difference whether this reference is a field (member) for the class which I update each time with the new element, or whether I just make a new temporary reference? Code example below
这个引用是否是每次使用new元素更新的类的字段(成员),或者我是否只是创建一个新的临时引用,这有什么不同吗?代码示例如下
for (element e : list) {
if (e.getID() == searchID) {
//reassigning value of the reference in the class
mElementToRemove = e;
break;
}
}
list.remove(mElementToRemove);
VS.
for (element e : list) {
if (e.getID() == searchID) {
//(new reference)
Element mElementToRemove = e;
break;
}
}
list.remove(mElementToRemove);
Perhaps there isn't a difference in terms of which is better style or which is more performant?
也许没有区别哪个是更好的风格或哪个更高效?
Thanks!
*edit . I mistyped the bottom example. For full context I meant:
*编辑。我错误地输入了最底层的例子。完整的上下文我的意思是:
private void removeThisId(int searchID) {
Element ElementToRemove = e;
for (element e : list) {
if (e.getID() == searchID) {
//(new reference)
mElementToRemove = e;
break;
}
}
list.remove(mElementToRemove);
}
2 个解决方案
#1
1
In the second example, the Element
will be out of scope by the end of the loop, so that won't even compile.
在第二个示例中,Element将在循环结束时超出范围,因此甚至不会编译。
Have you thought about using an Iterator
?
你有没有想过使用迭代器?
for(Iterator<Element> i = list.iterator(); i.hasNext(); ) {
if(i.next().getId() == searchID) {
i.remove();
}
}
No concurrent modification errors, no having to declare temp variables.
没有并发修改错误,不必声明临时变量。
And if you are guaranteed to only have at most one searchID
in the List (why not use Set
then?), then you can exit early by inserting break;
after the remove call.
如果你保证列表中最多只有一个searchID(为什么不使用Set呢?),那么你可以通过插入break来提前退出;删除电话后。
#2
0
Premature optimization is the root of all evil - Donald Knuth
过早的优化是所有邪恶的根源 - 唐纳德克努特
What the above means in your case is that, unless you have a compelling reason to optimize, do not contemplate optimization.
在您的情况下,上述意味着,除非您有一个令人信服的理由进行优化,否则不要考虑优化。
#1
1
In the second example, the Element
will be out of scope by the end of the loop, so that won't even compile.
在第二个示例中,Element将在循环结束时超出范围,因此甚至不会编译。
Have you thought about using an Iterator
?
你有没有想过使用迭代器?
for(Iterator<Element> i = list.iterator(); i.hasNext(); ) {
if(i.next().getId() == searchID) {
i.remove();
}
}
No concurrent modification errors, no having to declare temp variables.
没有并发修改错误,不必声明临时变量。
And if you are guaranteed to only have at most one searchID
in the List (why not use Set
then?), then you can exit early by inserting break;
after the remove call.
如果你保证列表中最多只有一个searchID(为什么不使用Set呢?),那么你可以通过插入break来提前退出;删除电话后。
#2
0
Premature optimization is the root of all evil - Donald Knuth
过早的优化是所有邪恶的根源 - 唐纳德克努特
What the above means in your case is that, unless you have a compelling reason to optimize, do not contemplate optimization.
在您的情况下,上述意味着,除非您有一个令人信服的理由进行优化,否则不要考虑优化。