如果我使用以下值使用 fore-each-template
- foreCtrl+空格键回车在第 5 行输入
- foreCtrl+空格键回车TabTabDown在第8行输入
- foreCtrl+空格键回车TabTabDownDown在第11行输入
将生成以下代码(由 Eclipse 4.2 生成)
01 public static String[] c = new String[]{"hi"};
02 public static void test() {
03 String[] a = new String[]{"hi"};
04 int[] b = new int[]{2};
05 for (String string : a) {
06 // 1. fine
07 }
08 for (int i : b) {
09 // 2. fine too
10 }
11 for (iterable_type iterable_element : c) {
12 // 3. not resolved?
13 }
14 }
现在问题:
- 为什么数组
c
不能解析它的数组类型和名称? - 这可能是 Eclipse 中的错误吗?
最佳答案
在模板中一段时间,
while (${condition:var(boolean)}) {
${line_selection}${cursor}
}
${condition:var(boolean)}
确实匹配成员和静态成员。
请注意,var 的内容辅助说:
${id:var(type[,type]*)} Evaluates to a field, local variable or parameter visible in the current scope that is a subtype of any of the given types. If no type is specified, any non- primitive variable matches.
在foreach的模板中,模板变量不同:
for (${iterable_type} ${iterable_element} : ${iterable}) {
${cursor}
}
${iterable}
变量的文档指出:
A proposal for an iterable (array or java.lang.Iterable)
没有具体说明是否应该提议(静态)成员。
${iterable} Evaluates to a proposal for an iterable or array visible in the current scope.
所以根据文档,这实际上可能是一个错误。其实已经被举报了here .
关于java - Foreach 协助使用类变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18331872/