A JBoss Project
Red Hat

Tools Tip#1 - Effective editing of enhanced for loops in Eclipse

posted by Max Rydahl Andersen on Dec 10, 2014.

tip jbosscentral

Comments

When you have have typed collections in your code like:

public void process(List<Product> products) {
  List<Customer> customers = getCustomers();
  // cursor here
}

Most know that you can use the foreach template via normal content assist to quickly generate code like this:

public void process(List<Product> products) {
  List<Customer> customers = getCustomers();
  for(Customer c : customers) {
    // cursor here
  }
}

But the foreach template will always choose the nearest collection, making it impossible to use with for example the products parameter in the above example.

The solution is to use the 'enhanced for loop' Quick Fix.

It is available when you have type the name of the collection and use the Ctrl+1 (Cmd+1 if on OS X) shortcut.

To illustrate it, here is a small video of it in Eclipse.

Hope you enjoyed this little tip!

Thanks to Xavier Coulon for asking the question and giving the idea for this Tip blog, and thanks to Markus Keller (JDT UI lead) for reminding me about the Quick Fix solution.

Have fun,
Max Rydahl Andersen
@maxandersen

Latest posts

JBoss Tools 4.29.1.Final for Eclipse 2023-09

by Stéphane Bouchet on Jun 13, 2024.

JBoss Tools 4.29.0.Final for Eclipse 2023-09

by Stéphane Bouchet on Nov 02, 2023.

JBoss Tools 4.28.0.Final for Eclipse 2023-06

by Stéphane Bouchet on Jul 03, 2023.

JBoss Tools for Eclipse 2023-06M2

by Stéphane Bouchet on Jun 05, 2023.

JBoss Tools 4.27.0.Final for Eclipse 2023-03

by Stéphane Bouchet on Apr 07, 2023.

Looking for older articles ? See the Archived entries.
back to top