J2SE 1.5 新功能特性:新的For循环
2008-02-23 09:43:32来源:互联网 阅读 ()
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:
- List names = new ArrayList();
- names.add("a");
- names.add("b");
- names.add("c");
- for (Iterator it = names.iterator(); it.hasNext(); ) {
- String name = (String)it.next();
- System.out.println(name.charAt(0));
- }
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:
- String[] moreNames = { "d", "e", "f" };
- for (int i = 0; i < moreNames.length; i )
- 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?
- List names = new ArrayList();
- names.add("a");
- names.add("b");
- names.add("c");
- for (Iterator it = names.iterator(); it.hasNext(); ) {
- String name = (String)it.next();
- System.out.println(((String)it.next()).charAt(0));
- }
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<String> names = new ArrayList<String>();
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.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系: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