J2SE 1.5 新功能特性:新的For循环(2)

2008-02-23 09:43:32来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折



Once you have stored objects in a parameterized List, you can iterate through them using the foreach loop:

for (String name: names)

You read this statement as, "for each String name in names." The Java VM executes the body of the foreach loop once for each object in the names collection. Each time through the loop, Java assigns the next object in turn to a local reference variable called name. You must declare this reference variable as a String type--the type to which you bound the names collection.

The foreach loop is succinct. There is no need to cast; Java does the cast for you. You can use the name reference within the body of the loop as a String reference. You cannot use the name reference outside the body of the foreach loop.

Using Foreach with Arrays


You saw how the foreach loop allows you to iterate over collection class types. Sun also modified Java to allow you to iterate through arrays using foreach. The syntax is exactly the same:

  1. String[] moreNames = { "d", "e", "f" };
  2. for (String name: moreNames)
  3. System.out.println(name.charAt(0));


Supporting Foreach in Your Own Class


Suppose you are building a Catalog class. A Catalog object collects any number of Product objects. You store these objects using an ArrayList instance variable defined in Catalog. Code working with a Catalog object will frequently need to iterate through the entire list of products, to do something with each object in turn.

Here is how the Catalog class might look:

  1. import java.util.*;
  2. class Catalog {
  3. private List<Product> products = new ArrayList<Product>();
  4. void add(Product product) {
  5. products.add(product);
  6. }
  7. }



The Product class includes a method that allows you to discount the price on a product:

  1. class Product {
  2. private String id;
  3. private String name;
  4. private BigDecimal cost;
  5. Product(String id, String name, BigDecimal cost) {
  6. this.id = id;
  7. this.name = name;
  8. this.cost = cost;
  9. }
  10. void discount(BigDecimal percent) {
  11. cost = cost.multiply(new BigDecimal("1.0").subtract(percent));
  12. }
  13. public String toString() {
  14. return id ": " name " @ " cost;
  15. }
  16. }


A (Poor) Solution


To allow client code to work with all products, you could create a method in Catalog that returned the ArrayList of products:

  1. // don't do this
  2. List<Product> getProducts() {
  3. return products;
  4. }



Client code could iterate through the list however they chose. However, returning a collection to a client is a bad idea. By giving the client your collection, you have relinquished any control over the contents of that collection. Client code could add or remove elements to the collection without the knowledge of the Catalog object. You've also burdened the client with more work than necessary.

The Iterable Interface


Instead, you can designate the Catalog class as being "iterable." Making a class iterable tells clients that they can iterate through its contents using a foreach loop. Sun has added to Java a new interface, java.lang.Iterable, that allows you to mark your classes as iterable:

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:20分钟熟悉猛虎脾气-JDK1.5新特性介绍

下一篇:闲来无事,写了个删除文件夹的java类