简体   繁体   English

Intellij IDEA 生成 for-each/for 键盘快捷键

[英]Intellij IDEA generate for-each/for keyboard shortcut

是否有生成 foreach 和 for 循环的键盘快捷键?

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.可能还有更多,只需在帮助文档中查找“实时模板”即可。

Pressing Ctrl + J opens the list of live templates within your code context.Ctrl + J打开代码上下文中的实时模板列表。 Then type 'itin'.然后输入'itin'。

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:除了其他答案之外,您甚至不必使用 Ctrl + J (但很适合检查您的可能性..)只需开始输入并按 Enter 键:

Foreach: type " iter " then press Enter. Foreach:输入“ iter ”,然后按 Enter 键。

For loop: type " fori " then press Enter. For 循环:输入“ fori ”,然后按 Enter。

Idea will recognize the context and suggest the most likely var to iter through, if you like the given options, just press Enters! Idea 将识别上下文并建议最有可能迭代的变量,如果您喜欢给定的选项,只需按 Enter!

Of course, have a look in the help at the default keymap references.当然,请查看默认键盘映射参考的帮助。 Type one of the following and hit "tab":键入以下内容之一并点击“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

Create for loop from an existing list statement:从现有列表语句创建 for 循环:

  1. Highlight the list突出显示列表
  2. Cmd + Alt + J or Ctrl + Alt + J Cmd + Alt + JCtrl + Alt + J
  3. Choose Iterate Iterable from the dropdown ( i )从下拉菜单中选择 Iterate Iterable ( i )

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

You can also use Surround with!您还可以使用环绕声!
Mac : Command + Option + T Mac:命令+选项+ T
Windows : Ctrl + Alt + T视窗: Ctrl + Alt + T
https://www.jetbrains.com/phpstorm/webhelp/surrounding-blocks-of-code-with-language-constructs.html https://www.jetbrains.com/phpstorm/webhelp/surrounding-blocks-of-code-with-language-constructs.html

There is also plenty of live templates in the internet !互联网上也有大量的实时模板! you can just google it.你可以谷歌一下。

You can use Postfix Completion too.您也可以使用 Postfix Completion。 Press Ctrl + Alt + S to open the Settings.Ctrl + Alt + S打开设置。 For example:例如:

int[] list = {1, 2, 3};    
list.for -> for(int i : list) {}

You can create your own live template, for example, I created this for iterating over an array:您可以创建自己的实时模板,例如,我创建它是为了迭代数组:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM