Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5989513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:06:44+00:00 2026-05-22T23:06:44+00:00

Could someone please explain what the for loop in this class does? Specifically the

  • 0

Could someone please explain what the for loop in this class does? Specifically the part with (String person : people)

import java.util.Scanner;

/**
 * This program uses the startsWith method to search using
 * a partial string
 * 
 *
 */
public class PersonSearch {

public static void main(String[] args){
    String lookUp;  //To hold a lookup string

    //Create an array of names
    String[] people= {"Cutshaw, Will", "Davis, George",
            "Davis, Jenny", "Russert, Phil", 
            "Russel, Cindy", "Setzer, Charles",
            "Smathers, Holly", "Smith, Chris", 
            "Smith, Brad", "Williams, Jean" };

    //Create a Scanner object for keyboard input
    Scanner keyboard=new Scanner(System.in);

    //Get a partial name to search for
    System.out.println("Enter the first few characters of "+
            "the last name to look up: ");
    lookUp=keyboard.nextLine();

    //Display all of the names that begin with the
    //string entered by the user
    System.out.println("Here are the names that match:");
    for(String person : people){
        if (person.startsWith(lookUp))
            System.out.println(person);

    }
}
}

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:06:44+00:00Added an answer on May 22, 2026 at 11:06 pm

    It’s called the foreach syntax. It works with arrays and Objects that implement Iterable.

    For arrays (as here) it’s equivalent to this code:

    for (int i = 0; i < people.length; i++) {
        person = people[i];
        // code inside loop
    }
    

    For Iterable<T> iterable (eg a List), it’s equivalent to:

    for (Iterator<T> i = iterable.iterator(); i.hasNext(); ) {
        T next = i.next();
        // code inside loop
    }
    

    This code pattern was so common, and added so little value, this abbreviated form of looping was officially made part of the java language in version 1.5 (aka “Java 5”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please explain the behavior of this class testCompile { /* * Sample
Could someone please explain the purpose of this option and what exactly it is
Could someone please explain, I do not exactly get the concept. What is a
Could someone please explain what's the difference between inheriting from ISerializable interface and declaring
I was hoping someone could explain the nuances of the __user macro used in
This is for Python 2.6. I could not figure out why a and b
There is something I don't quite understand right now. My main activity class creates
I'm new to PHP and don't understand what the point of <<<_END is. Could
OK, the question title is a bit crappy, but I didn't really know how
I am playing around with the fastCGI application found here . The following comment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.