dummies logo
Articles

Article Categories

close
TechnologyAcademics & The ArtsHome, Auto, & HobbiesBody, Mind, & SpiritBusiness, Careers, & Money
Collections

Collections

Explore all collections
close
BYOB (Be Your Own Boss)Be a Rad DadCareer ShiftingContemplating the CosmosFor Those Seeking Peace of MindFor the Aspiring AficionadoFor the Budding Cannabis EnthusiastFor the College BoundFor the Exam-Season CrammerFor the Game Day Prepper
Custom Solutions
Dummies AI
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
forward arrow
  • Custom Solutions
  • Dummies AI
    • Log In
    Explore Book
    You can make an enhanced for loop to step through a bunch of values, including an array’s values. Let’s take a look at an enhanced for loop that steps through an array’s values.

    To see such a loop, start with this code. The loop looks something like this:

    for (int roomNum = 0; roomNum < 10; roomNum++) {

    out.println(guestsIn[roomNum]);

    }

    To turn this into an enhanced for loop, you make up a new variable name. (What about the name howMany? Nice name.) Whatever name you choose, the new variable ranges over the values in the guestsIn array.

    for (int howMany : guestsIn) {

    out.println(howMany);

    }

    This enhanced loop uses this format.

    for (<em>TypeName variableName</em> : <em>RangeOfValues</em>) {

    <em>Statements</em>

    }

    The RangeOfValues can belong to an enum type. Here, the RangeOfValues belongs to an array.

    Enhanced for loops are nice and concise. But don’t be too eager to use enhanced loops with arrays. This feature has some nasty limitations. For example, the new howMany loop doesn’t display room numbers. Room numbers are avoided because the room numbers in the guestsIn array are the indices 0 through 9. Unfortunately, an enhanced loop doesn’t provide easy access to an array’s indices.

    And here’s another unpleasant surprise. Start with the following loop:

    for (int roomNum = 0; roomNum < 10; roomNum++) {

    guestsIn[roomNum] = diskScanner.nextInt();

    }

    Turn this traditional for loop into an enhanced for loop, and you get the following misleading code:

    for (int howMany : guestsIn) {

    howMany = diskScanner.nextInt(); <strong>/Don't do this</strong>

    }

    The new enhanced for loop doesn’t do what you want it to do. This loop reads values from an input file and then dumps these values into the garbage can. In the end, the array’s values remain unchanged.

    It’s sad but true. To make full use of an array, you have to fall back on Java’s plain old for loop.

    About This Article

    This article is from the book: 

    Beginning Programming with Java For Dummies

    About the book author:

    Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

    This article can be found in the category: 

    Java
    Trending
    Captain America For Dummies Cheat Sheet
    How to Make a Margarita
    10 Common Misconceptions About Somatic Exercises
    Creating Custom Excel Templates
    Fantastic Four For Dummies Cheat Sheet
    From Category
    Java
    Important Features of the Java Language
    How to Download and Install TextPad
    How to Install JavaFX and Scene Builder
    Getting a Value from a Method in Java
    A Few Things about Java GUIs

    Quick Links

    About For Dummies
     Terms of Use
    Copyright © 2000-2025 by John Wiley & Sons, Inc., or related companies. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
    © 2025 MARVEL

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant