赛迪网 > IT技术 乱谈 > 文章
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

J2SE 1.5 新功能特性:For循环

发布时间:2006.03.14 16:26     来源:CSDN    作者:Jeff Langr

Like many Java developers you are probably working with the beta for J2SE 1.5. Here is another new technique to use with the beta. Looping through a collection of objects and doing something with each object is one of the most common programming idioms. You've no doubt coded such a loop many times: 

  1. List names = new ArrayList();
  2. names.add("a");
  3. names.add("b");
  4. names.add("c");
  5. for (Iterator it = names.iterator(); it.hasNext(); ) {
  6.    String name = (String)it.next();
  7.    System.out.println(name.charAt(0));
  8. }


Instead of using a for loop, you might have used a while loop. You might have used an Enumeration, instead of an Iterator, if you were working with the older Vector class. Or, you might have used a for loop and a counter, accessing each element using the get(int index) method of the List interface. 

If you were to use an array, this is how you would typically iterate through each of its elements: 

  1. String[] moreNames = { "d""e""f" };
  2. for (int i = 0; i < moreNames.length; i++)
  3.    System.out.println(moreNames[i].charAt(0));



It's not difficult to iterate through a collection, but it is a very repetitive operation. Iteration requires you to type a good amount of characters. Granted, the template feature of most IDEs will eliminate your need to type so much. But, as you saw, there are many ways to iterate through a loop. 

The existence of several different iteration techniques costs you more time. For each iteration you encounter, you must spend a few extra seconds digesting the iteration form. If it deviates from the form you're used to seeing, you must examine it even closer. Why did the developer choose to code the iteration differently? 

It's also easy to make a mistake in coding your iterator. Ever accidentally coded something like this? 

  1. List names = new ArrayList();
  2. names.add("a");
  3. names.add("b");
  4. names.add("c");
  5. for (Iterator it = names.iterator(); it.hasNext(); ) {
  6.    String name = (String)it.next();
  7.    System.out.println(((String)it.next()).charAt(0));
  8. }


I have. In a small section of code, it's easy to spot the error. In a larger section of code, it's not as easy to spot. 

The foreach Loop


Java 1.5 introduces a new way of iterating over a collection of objects. The foreach loop is also known as the enhanced for loop. It is a new syntactical construct designed to simplify iteration. The foreach loop should make iteration more consistent, but only if and when everyone starts using it. 

Effective use of the foreach loop depends on using Java 1.5's parameterized types, also known as generics. So that you can understand the code in this article, I'll explain how to use parameterized types. 

Here is how you might construct and iterate a list of names in Java 1.5: 

List names = new ArrayList();
names.add("a");
names.add("b");
names.add("c");

for (String name: names)
   System.out.println(name.charAt(0));

There are two important things in this bit of code: first, the use of a generic list, and second, the use of the new foreach loop. 

To construct an object of the parameterized ArrayList type, you bind the ArrayList to a class. In the example, you bind the ArrayList to the String class. You also bind the List reference to the String class. You are now restricted to adding only String objects to the list. If you insert, for example, a Date object in the names collection, your code will not compile. When you retrieve objects from the list, you need not cast. Java knows that the list contains only String objects. It does the casting for you, behind the scenes. 

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 products = new ArrayList();
  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.    
  11.    void discount(BigDecimal percent) {
  12.       cost = cost.multiply(new BigDecimal("1.0").subtract(percent));
  13.    }
  14.    
  15.    public String toString() {
  16.       return id + ": " + name + " @ " + cost;
  17.    }
  18. }


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 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: 

  1. package java.lang;
  2. import java.util.Iterator;
  3. public interface Iterable {
  4.     Iterator iterator();
  5. }


To implement this interface in Catalog, you must code an iterator method. This method must return an Iterator object that is bound to the Product type. 

The Catalog class stores all of its Product objects in an ArrayList. The easiest way to provide an Iterator object to a client is to simply return the Iterator object from the products ArrayList itself. Here is the modified Catalog class: 

  1. class Catalog implements Iterable {
  2.    private List products = new ArrayList();
  3.    void add(Product product) {
  4.       products.add(product);
  5.    }
  6.    
  7.    public Iterator iterator() {
  8.       return products.iterator();
  9.    }
  10. }


Note that the Iterator, the List reference, and the ArrayList are all bound to the Product type. 

Once you have implemented the java.lang.Iterable interface, client code can use the foreach loop. Here is a bit of example code: 
  1. Catalog catalog = new Catalog();
  2. catalog.add(new Product("1""pinto"new BigDecimal("4.99")));
  3. catalog.add(new Product("2""flounder"new BigDecimal("64.88")));
  4. catalog.add(new Product("2""cucumber"new BigDecimal("2.01")));
  5. for (Product product: catalog)
  6.    product.discount(new BigDecimal("0.1"));
  7.    
  8. for (Product product: catalog)
  9.    System.out.println(product);



Summary


The foreach loop provides a simple, consistent solution for iterating arrays, collection classes, and even your own collections. It eliminates much of the repetitive code that you would otherwise require. The foreach loop eliminates the need for casting as well as some potential problems. The foreach loop is a nice new addition to Java that was long overdue. I greatly appreciate the added simplicity in my source code. 

Trying it Out


You can download the current beta version of J2SE 1.5 from Sun's web site.


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· 最新版J2SE 1.4 (03-14) · J2SE 1.4.1 for Mac OS X (03-14)
· J2SE 1.5测试版 (03-14) · J2SE下一个版本的源代码公开 (03-14)
· J2SE兼容性测试源代码发布 (03-14) · J2SE软件开发包漏洞 (03-14)
· J2SE 6.0初见雏形 (03-14) · NetBeans新版同步J2SE变身5.0 (03-14)
· 克服J2SE 1.3 ~ 1.4不兼容问题 (03-14) · 升级到J2SE 5平台的5大理由 (03-14)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
· 北京新规不能霸王硬上弓 网店牌照缓期执行
· 软件外包之变的新台阶: 提高全球交付能力
· ERP案例分析 SaaS带来冲击 IT服务商面临挑战
· 通方期货CRM解决方案 房地产行业CRM解决方案
  IT博客 ·曾剑秋·项立刚·Java学习·网管
  IT技术论坛 ·开发·网管·安全·数据库·系统