java - Foreach 协助使用类变量?

标签 java eclipse eclipse-templates

如果我使用以下值使用 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 }

现在问题:

  1. 为什么数组 c 不能解析它的数组类型和名称?
  2. 这可能是 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)

没有具体说明是否应该提议(静态)成员。

编辑:this documentation page指出

${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/

相关文章:

java - 时间/日期对话框。尝试添加按钮。 Eclipse 安卓

java - 调用 firePropertyChange() 的 setter 的 Eclipse 模板

java - getter 和 setter 的 Eclipse 模板变量

java - 打印小程序 GUI

java - 从一个 JFrame 移动到另一个

eclipse - 带有 Java Server Faces 1.2 的 Maven Java EE 配置标记

java - Eclipse java 中出现包不存在的错误 [ERROR]

java - 如何使用 Eclipse 模板自动插入类表示法?

java - 即使发送了属性,JSP 代码也显示 null

java - 为什么 doAction() 提前完成了 done() ?