J2SE 1.5 新功能特性:新的For循环(2)
2008-02-23 09:43:32来源:互联网 阅读 ()
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:
- String[] moreNames = { "d", "e", "f" };
- for (String name: moreNames)
- 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:
- import java.util.*;
- class Catalog {
- private List<Product> products = new ArrayList<Product>();
- void add(Product product) {
- products.add(product);
- }
- }
The Product class includes a method that allows you to discount the price on a product:
- class Product {
- private String id;
- private String name;
- private BigDecimal cost;
- Product(String id, String name, BigDecimal cost) {
- this.id = id;
- this.name = name;
- this.cost = cost;
- }
- void discount(BigDecimal percent) {
- cost = cost.multiply(new BigDecimal("1.0").subtract(percent));
- }
- public String toString() {
- return id ": " name " @ " cost;
- }
- }
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:
- // don't do this
- List<Product> getProducts() {
- return products;
- }
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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
