Using for/in to enhance the cycle in Java 5.0

Source: Internet
Author: User

The for/in loop is often called an enhanced for or foreach, which is an extremely convenient feature in Java 5.0. It doesn't actually offer any new functionality, but it obviously makes some everyday coding tasks easier. In this article, you'll learn a lot about this, including using for/in to iterate through arrays and collections, and how to use it to avoid unnecessary (or just annoying) type conversions. You'll also learn how to implement For/in, learn some details about the new iterable interface, and even learn how to get your own custom objects traversed with this new construct. Finally, you'll learn what for/in can't do to make sure you understand when choosing the original for is the right choice.

As short as possible.

This is one of the most basic principles that senior computer programs know: Because shorter means less typing, so shorter nature is better. This philosophy created the Ides such as VI, in this kind of IDE, like: wq! and 28G such commands have a rich meaning. This philosophy also leads to some of the most cryptic code, for example, the variable ar represents the Agile Runner (also may be Argyle, or Atomic reactor etc., in short, you know).

Sometimes, when trying to achieve a short time, the programmer will put the clarity of the brain behind. In other words, too short and too cumbersome code can make people feel miserable. A variable named Theatomicreactorlocatedinphiladelphia is as annoying and inconvenient as a variable named AR. There's got to be a happy solution, right?

This pleasing method (at least I think so) is based on finding a convenient way to accomplish something, not a short one. As a good example of such a solution, Java 5.0 introduced the new for loop, which I call for/in. It is also called a foreach, sometimes called an enhanced for, but these refer to the same construct. Whatever you call it, For/in makes the code simpler, as you'll see in this article.

Do not use iterator

The basic difference between using for/in and normal for is that you do not have to use a counter (usually called I or count) or iterator. See Listing 1, which shows a iterator for loop for use:

Listing 1. For loops, old school style

public void testForLoop(PrintStream out) throws IOException {
  List list = getList(); // initialize this list elsewhere

  for (Iterator i = list.iterator(); i.hasNext(); ) {
   Object listElement = i.next();
   out.println(listElement.toString());

   // Do something else with this list element
  }
}

Note: If you have been reading my article on Tiger's new features (see Resources), you will know that I often thank O ' Reilly Media, Inc., because they allow me to publish code examples from my other books in this article. This means that the code you get has passed more tests, more reviews, and much more than I can offer you. So thanks again O ' Reilly, if you want to learn more about Tiger, please refer to some of the books I've written, which are listed in the Reference Resources section with complete links and more details.

If you're looking forward to a detailed explanation of how to turn this code into a new for/in loop, I'm afraid I'm going to disappoint you. Listing 2 shows the code in Listing 1, rewritten with for/in, and you should be quite familiar with it. See the code listing below, and I'll explain the for/in loop in as much detail as possible (but it's still hard to make a chapter).

Listing 2. Convert into For/in

public void testForInLoop(PrintStream out) throws IOException {
  List list = getList(); // initialize this list elsewhere

  for (Object listElement : list) {
   out.println(listElement.toString());

   // Do something else with this list element
  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.