For each loop in java

Source: Internet
Author: User

It can be used to process each element in the array in sequence (other types of element sets can also be) without being distracted by specifying the lower value. The statement format of this for loop is: for (variable: collection) statement, which defines a variable for saving every element in the set and executing the corresponding statement or statement block. The Set expression must be an array or a class object that implements the iterable interface, such as arrayList. For example: for (int element: a) Every M. out. println (element); print each element of array a, one element occupies a row. If you do not want to traverse the entire set, or you need to operate the value inside the loop, you need to use the traditional for loop. 1 import java. util. *; 2 3/** 4 * This program tests the Employee class. 5 * @ version 1.11 6 * @ author Cay Horstmann 7 */8 public class EmployeeTest 9 {10 public static void main (String [] args) 11 {12 // fill the staff array with three Employee objects13 Employee [] staff = new Employee [3]; 14 15 staff [0] = new Employee ("Carl Cracker ", 75000,198 7, 12, 15); 16 staff [1] = new Employee ("Harry Hacker", 50000,198 9, 10, 1 ); 17 staff [2] = new Employee ("Tony Tester", 40000,199 0, 3, 15); 18 19 // raise everyone's salary by 5% 20 for (Employee e: staff) 21 e. raiseSalary (5); 22 23 // print out information about all Employee objects24 for (Employee e: staff) 25 System. out. println ("name =" + e. getName () + ", salary =" + e. getSalary () + ", hireDay =" 26 + e. getHireDay (); 27} 28} 29 30 class Employee31 {32 public Employee (String n, double s, int year, int month, int day) 33 {34 name = n; 35 salary = s; 36 GregorianCalendar calendar = new GregorianCalendar (year, month-1, day); 37 // GregorianCalendar uses 0 for January38 hireDay = calendar. getTime (); 39} 40 41 public String getName () 42 {43 return name; 44} 45 46 public double getSalary () 47 {48 return salary; 49} 50 51 public Date getHireDay () 52 {53 return hireDay; 54} 55 56 public void raiseSalary (double byPercent) 57 {58 double raise = salary * byPercent/100; 59 salary + = raise; 60} 61 62 private String name; 63 private double salary; 64 private Date hireDay; 65}

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.