我在構造函數中創建for循環以同時迭代地圖和數組時遇到了一些麻煩。 Here,這表明這不能用增強for循環完成。在for循環中同時迭代地圖和數組
我有這樣的事情,這會引發編譯器錯誤。基本上,這個類有一個我想通過構造函數來填充的Map,它將一個集合和一個可變數目的整數作爲參數。
var-arg表達式求值爲一個整數數組,所以我試圖將兩個增強迭代器放在同一個循環中,但沒有奏效。
private final Map<Module, Integer> modules = new HashMap<Module, Integer>();
AssemblyType(Collection<Module> modules, int... units) {
int i = 0;
for (Module module : modules, int i : units) {
this.modules.put(module, units[i]);
}
}
感謝您對如何着手的任何想法。
&&操作符是否在for循環中工作? – 2009-11-14 03:34:38