Java的:"匿名"数组中的for-each循环
问题描述:
当我试图在一些特殊的for循环我认为Java的似乎不喜欢把一个匿名数组权为源动力的一个的for-each循环:
While I was trying something special in for loop I recognized that Java doesn't seem to like putting an anonymous array right as the source for a for-each-loop:
for (String crt : {"a","b","c"} ) {
doSomething();
}
其实没有,而
String[] arr = {"a","b","c"};
for (String crt : arr ) {
doSomething();
}
确实
即使铸造数组的String []于事无补。当将光标移到第一个版本,日食告诉我:
Even casting the array to String[] doesn't help. When moving the cursor over the first version, eclipse tells me:
类型不匹配:不能从String转换[]为String
而意为CRT
这是一个错误吗?
答
这将工作:
for (String crt : new String[]{"a","b","c"} ) {
doSomething();
}