Intellij IDEA generate for-each/for keyboard shortcut

Intellij Idea

Intellij Idea Problem Overview


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

Intellij Idea Solutions


Solution 1 - Intellij Idea

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.

Solution 2 - Intellij Idea

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

Solution 3 - Intellij Idea

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!

Solution 4 - Intellij Idea

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

Solution 5 - Intellij Idea

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

Solution 6 - Intellij Idea

You can also use Surround with!
Mac : Command+Option+T
Windows : Ctrl+Alt+T
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.

Solution 7 - Intellij Idea

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) {}

Solution 8 - Intellij Idea

You can create your own live template, for example, I created this for iterating over an array:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlideView Question on Stackoverflow
Solution 1 - Intellij Idead-liveView Answer on Stackoverflow
Solution 2 - Intellij IdeaTroyJView Answer on Stackoverflow
Solution 3 - Intellij IdeaBlondCodeView Answer on Stackoverflow
Solution 4 - Intellij IdeaduffymoView Answer on Stackoverflow
Solution 5 - Intellij Idead4krisView Answer on Stackoverflow
Solution 6 - Intellij IdeaMeabedView Answer on Stackoverflow
Solution 7 - Intellij IdeaZizouView Answer on Stackoverflow
Solution 8 - Intellij IdeaArefeView Answer on Stackoverflow