Intellij IDEA generate for-each/for keyboard short

2019-01-29 17:19发布

Is there a keyboard shortcut generating a foreach and also for loop?

7条回答
闹够了就滚
2楼-- · 2019-01-29 17:29

Of course, have a look in the help at the default keymap references. Type one of the following and hit "tab":

iter Iteration according to Java SDK 1.5 style
inst Check object type with instanceof and downcast it
itco Iterate elements of java.util.Collection
itit Iterate elements of java.util.Iterator
itli Iterate elements of java.util.List
查看更多
仙女界的扛把子
3楼-- · 2019-01-29 17:29

You can use Postfix Completion too. Press Ctrl + Alt + S to open the Settings. For example:

int[] list = {1, 2, 3};    
list.for -> for(int i : list) {}
查看更多
Luminary・发光体
4楼-- · 2019-01-29 17:34

Aditionally to other answers, you don't even have to use Ctrl + J (but nice for checking what are your possibilities..) Just start type and press Enter:

Foreach: type "iter" then press Enter.

For loop: type "fori" then press Enter.

Idea will recognize the context and suggest the most likely var to iter through, if you like the given options, just press Enters!

查看更多
爷、活的狠高调
5楼-- · 2019-01-29 17:38

you can use 'live templates' to generate several types of code snippets, loop iteration is done by following -

iter    Iterate (for each..in) 
itin    Iterate (for..in) 
itli    Iterate over a List
itar    Iterate elements of array 
ritar   Iterate elements of array in reverse order 

There are probably many more, just lookup 'Live Templates' in help documentation.

查看更多
Viruses.
6楼-- · 2019-01-29 17:40

Pressing Ctrl+J opens the list of live templates within your code context. Then type 'itin'.

查看更多
趁早两清
7楼-- · 2019-01-29 17:47

Create for loop from an existing list statement:

  1. Highlight the list
  2. Cmd+Alt+J or Ctrl+Alt+J
  3. Choose Iterate Iterable from the dropdown (i)

https://www.jetbrains.com/phpstorm/help/creating-code-constructs-using-surround-templates.html

查看更多
登录 后发表回答