thinking in java 笔记 3

本文介绍JDK7中引入的新特性,包括使用foreach语法简化整数序列遍历的方法,以及如何在switch语句中使用字符串。通过具体示例展示了这些新特性的实用性和提高代码可读性的方式。

p.97 foreach syntax

Many for statements involve stepping through a sequence of integral values, like this:

for(int i = 0; i < 100; i++)
For these, the foreach syntax won’t work unless you want to create an array of int first. To
simplify this task, I’ve created a method called range( ) in net.mindview.util.Range that
automatically generates the appropriate array. My intent is for range( ) to be used as a
static import:
//: control/ForEachInt.java
import static net.mindview.util.Range.*;
import static net.mindview.util.Print.*;
public class ForEachInt {
public static void main(String[] args) {
for(int i : range(10)) // 0..9
printnb(i + " ");
print();
for(int i : range(5, 10)) // 5..9
printnb(i + " ");
print();
for(int i : range(5, 20, 3)) // 5..20 step 3
printnb(i + " ");
print();
}
} /* Output:
0 1 2 3 4 5 6 7 8 9
5 6 7 8 9
5 8 11 14 17
*///:~
The range( ) method has been overloaded, which means the same method name can be
used with different argument lists (you’ll learn about overloading soon). The first overloaded
form of range( ) just starts at zero and produces values up to but not including the top end
of the range. The second form starts at the first value and goes until one less than the second,
and the third form has a step value so it increases by that value. range( ) is a very simple
version of what’s called a generator, which you’ll see later in the book.
Note that although range( ) allows the use of the foreach syntax in more places, and thus
arguably increases readability, it is a little less efficient, so if you are tuning for performance
you may want to use a profiler, which is a tool that measures the performance of your code.
You’ll note the use of printnb( ) in addition to print( ). The printnb( ) method does not

emit a newline, so it allows you to output a line in pieces.


p.104 swith

jdk7 对switch 字符串的支持

In the JDK 7 release, you can use a String object in the expression of a switch statement:

[java]  view plain  copy
  1. public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {  
  2.      String typeOfDay;  
  3.      switch (dayOfWeekArg) {  
  4.          case "Monday":  
  5.              typeOfDay = "Start of work week";  
  6.              break;  
  7.          case "Tuesday":  
  8.          case "Wednesday":  
  9.          case "Thursday":  
  10.              typeOfDay = "Midweek";  
  11.              break;  
  12.          case "Friday":  
  13.              typeOfDay = "End of work week";  
  14.              break;  
  15.          case "Saturday":  
  16.          case "Sunday":  
  17.              typeOfDay = "Weekend";  
  18.              break;  
  19.          default:  
  20.              throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);  
  21.      }  
  22.      return typeOfDay;  
  23. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值