简体   繁体   English

变量初始化

[英]Variable initialisation

Which is a more efficient way to declare the movie object, inside a loop or outside assigning new values each iteration? 在循环内还是在每次迭代外部分配新值时,哪种更有效的方法来声明影片对象?

ArrayList<Movie> localMovies = new ArrayList<Movie>();    
for (File file : listOfFiles) {
    Movie movie = new Movie();
    movie.setTitlePattern(filePattern);
    localMovies.add(movie);
}

Or 要么

ArrayList<Movie> localMovies = new ArrayList<Movie>();
Movie movie = new Movie();    
for (File file : listOfFiles) {
    movie.setTitlePattern(filePattern);
    localMovies.add(movie);
}

I don't think efficiency is the key point here. 我认为效率不是这里的重点。 The key point is correctness . 关键是正确性

The two snippets do different things . 这两个片段执行不同的操作

  • The first version populates the list with different Movie objects. 第一个版本使用不同的Movie对象填充列表。

  • The second version populates the list with multiple references to a single Move object. 第二个版本使用对单个Move对象的多个引用填充列表。 You then proceed to change that object's title pattern. 然后,您继续更改该对象的标题模式。 So when the loop finishes you will have N references to the same object, and the title pattern for that object will be the will be the last one set. 因此,当循环结束时,您将有N个对同一对象的引用,并且该对象的标题模式将是最后一组。

Though it is not possible to say for sure without more context, the second version looks incorrect ... it doesn't do what (I think) you are trying to do. 尽管没有更多的上下文无法确定,但是第二个版本看起来不正确……它没有执行您要尝试执行的操作(我认为)。


Having said that, the "incorrect" version of your code is probably a bit faster. 话虽如此,代码的“不正确”版本可能会快一些。 Whether the difference is significant (worth worrying about) depends on the context. 差异是否显着(值得担心)取决于上下文。 And the question is moot anyway. 无论如何,这个问题还没有解决。

If you were to compare the performance of these two "correct" versions: 如果要比较这两个“正确”版本的性能:

ArrayList<Movie> localMovies = new ArrayList<Movie>();    
for (File file : listOfFiles) {
    Movie movie = new Movie();
    movie.setTitlePattern(filePattern);
    localMovies.add(movie);
}

and

ArrayList<Movie> localMovies = new ArrayList<Movie>();  
Movie movie;  
for (File file : listOfFiles) {
    movie = new Movie();
    movie.setTitlePattern(filePattern);
    localMovies.add(movie);
}

there would most likely be no detectable difference. 极有可能没有可检测到的差异。

You'd better initialize the Movie object inside the for loop unless you want the list to hold multiple references to just one single Movie object. 最好在for循环中初始化Movie对象,除非您希望列表仅包含对一个Movie对象的多个引用。 This has nothing to do with efficiency and all to do with bug-free coding. 这与效率无关,而与无错误编码有关。 Feel free to declare the Movie object before or inside the loop. 可以在循环之前或循环内部声明Movie对象。

For efficiency, the second one, as it does not have to call and execute the constructor each time. 为了提高效率,第二个代码因为不必每次都调用和执行构造函数。

For correctness, though, you probably want the first one, as the second one gives you many times the same object in the list. 但是,为了正确起见,您可能需要第一个对象,因为第二个对象会多次为您提供列表中的同一对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM