问题描述
我是Eclipse的新手,我主要用于Java。我以前使用过IntelliJ Idea,它可以选择一个扩展Iteratable(Collection,List等)的变量,并产生一个正确的foreach循环。我知道Eclipse与foreach模板类似,它猜测哪个变量要迭代,但是我不能将它与选定的变量相同。但是如果变量不在当前范围内,如果Eclipse猜测错误怎么办?
所以我想要做的是能够选择一个变量选择:
functionWhichReturnsList()(返回List< TypeOfItemsInList>)
结果:
(TypeOfItemsInList item:functionWhichReturnsList()){$ {cursor}
}
$
我通常按照以下步骤创建这样的代码:
调用函数并使用Ctrl-1创建一个持有返回值的局部变量:
列表< TypeOfItemsInList>列表= functionWhichReturnsList()
输入前面的[Ctrl-space]插入for循环(由于eclipse通常在构建循环时选择最接近的迭代):
列表< TypeOfItemsInList> list = functionWhichReturnsList()
for(TypeOfItemsInList item:list){
}
通过将光标放在列表变量上并键入Alt + Shift + I来对局部变量进行内联:
(TypeOfItemsInList item:functionWhichReturnsList()){
}
这不是最佳的, 。
I am new to Eclipse which I use primarily for Java. I have previously used IntelliJ Idea in which it is possible to select a variable which extends Iteratable (Collection, List etc) and have it produce a correct foreach loop.
I know Eclipse does something similar with the foreach template, where it guesses which variable to iterate over, but I can't get it to the same thing with a selected variable. But what if the variable is not in the current scope and what if Eclipse guesses wrong?
So what I am trying to do is being able to select a variable (or function which returns a variable) which implements Iterator and have it return:
Selection:
functionWhichReturnsList() (which returns List<TypeOfItemsInList>)
Result:
for (TypeOfItemsInList item : functionWhichReturnsList()) {
${cursor}
}
Any ideas?
I typically create code like this by following these steps:
Call the function and use Ctrl-1 to create a local variable holding the return value:
List<TypeOfItemsInList> list = functionWhichReturnsList()
Type fore[Ctrl-space] to insert the for loop (since eclipse usually chooses the closest iterable when constructing the loop):
List<TypeOfItemsInList> list = functionWhichReturnsList()
for (TypeOfItemsInList item : list) {
}
Inline the local variable by putting the cursor on the list variable and typing Alt+Shift+I:
for (TypeOfItemsInList item : functionWhichReturnsList()) {
}
It's not optimal, but it works.
这篇关于Eclipse中的“Surround with”-template:foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!