[英]Is there a better way to create an actionListener to JButtons in a for-loop?
[英]Which way to iterate for-loop is better?
我必須使用for-loop
進行迭代,但我想知道哪種方式更好。
1:
import java.util.ArrayList;
public class FindTime {
public static void main(String[] args) {
ArrayList tmpList = new ArrayList();
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
long startTime = System.nanoTime();
for (int i = 0,size=tmpList.size(); i < size; i++) {
System.out.println(tmpList.get(i).toString());
}
long endTime = System.nanoTime();
System.out.println("Time Duration ::->" + (endTime - startTime));
}
}
2:
import java.util.ArrayList;
public class FindTime {
public static void main(String[] args) {
ArrayList tmpList = new ArrayList();
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
long startTime = System.nanoTime();
for (int i = 0;i < tmpList.size(); i++) {
System.out.println(tmpList.get(i).toString());
}
long endTime = System.nanoTime();
System.out.println("Time Duration ::->" + (endTime - startTime));
}
}
在上文中,兩個for-loops
具有相同的內容,但只是循環條件的差異。 誰能告訴我上面的迭代實際發生了什么?
你錯了。 你專注於幾乎無所謂的方面; 這樣做,你寫了不好的代碼 - 在你的例子中!
首先,您不使用原始類型 。
你改為:
List<String> theWords = new ArrayList<>();
(請注意:使用List作為列表對象的實際類型也是更好的做法。無需將其背后的實現暴露給該列表的用戶)
Java有很好的“填充”列表的方法,比如
List<String> theWords = Arrays.asList("me", "didn't", "know", "that", "but", "could", "have");
然后; 你使用for-each來迭代各種集合/數組:
for (String aWord : theWords)
並且你不再擔心所有這些低級別的循環,包括整數和增量。
換句話說:Java不是C.我們在許多地方都有更好的抽象; 你最好關注那些; 因為他們照顧這些微妙之處。 含義:專注於編寫“更高級別”代碼 - 因為這會創建可讀代碼。 請放心:如果您遵循典型的“最佳實踐”(如上所述使用'for each'進行迭代循環) - 您無需擔心性能問題。 因為JVM和JIT最擅長優化...如果你使用它們提供給你的抽象!
努力做到聰明; 表達“低級別”的東西甚至可能產生負面影響; 因為它可能會阻止JIT進行其榮耀優化工作(可能不是在這種情況下)。
1更快,因為tmpList.size()只被調用一次。
2更清潔 ,更容易閱讀,因為它更簡單。
干凈
以下是最簡潔的,因為它易於閱讀,並在每個IDE中具有完整的語法高亮支持:
for ( String s : tmpList ) {
System.out.println(s);
}
最快的
以下是最快的性能,因為它使用本機數組並避免無關對象創建和getter方法調用:
String[] tmpList = String[24];
tmpList[0] = "me";
tmpList[1] = "you";
...
for ( int i = 0; i < tmpList.length; ++i ) {
System.out.println(tmpList[i]);
}
非常快
事實上,如果您有一個在編譯時已知的列表,那么最快的性能解決方案是展開您的循環。
System.out.println("me");
System.out.println("you");
...
一般來說KISS( 保持簡單愚蠢 )並僅在您遇到真正的性能問題時進行優化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.