开发者

Java: Explain what this for() loop parameter does

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 开发者_StackOverflow社区names that match:");
    for(String person : people){
        if (person.startsWith(lookUp))
            System.out.println(person);

    }
}
}

Thank you.


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").


It is the enhanced for loop. It has been there since Java 5.


lookUp is a string variable that contains the user's input.

Assuming the user input the name "George", it would contain the name "George".

people is an array of Strings, things like "Jimmy", "George", and "John"

In your For Loop, all of the strings in the people array are checked to see if they start with the string, "George".

If any of the names in people start with George, the full person string is printed out.

Strings that are printed might include, 'George Foreman' or 'George Brett'


It means for each person (element in the array of the type String) in people do as follows. It is the syntax for foreach in java.

Similar question(s) on Stackoverflow

  1. How does the java foreach loop work?


That function will do a string comparison on each person in the collection to see if the person string starts with the lookup string. For instance if person was hivie7510 and lookup was hivie then it would be true and print it out.


The for loop iterates over the people array in this example. Each instance is stored in a variable called person. If lookup stored "S" then the person.startWith(loopup) will check if the string person begins with S. Hence, the results would be

Setzer, Charles
Smathers, Holly
Smith, Chris
Smith, Brad

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜