在使用String.split方法分隔字符串时,分隔符如果用到一些特殊字符,可能会得不到我们预期的结果。
我们看jdk doc中说明
public String[] split(String regex)
Splits this string around matches of the given regular expression.
参数regex是一个 regular-expression的匹配模式而不是一个简单的String,他对一些特殊的字符可能会出现你预想不到的结果,比如测试下面的代码:
用竖线 | 分隔字符串,你将得不到预期的结果
String[] aa = “aaa|bbb|ccc”.split(“|”);
//String[] aa = “aaa|bbb|ccc”.split(“\\|”); 这样才能得到正确的结果
阅读全文 »
作者:jock
Tags: j2se
Annotation是J2SE提供的一种新语法,根据规范的说明,他的作用简单来说就是简化代码的维护。例如,EJB需要7个文件,一个XML文件来表示,通过Annotation,我们可以只需要维护源代码,其他工作都可以通过自动生成达到。这个做法跟Lomboz的效果比较一致,不过Lomboz是通过XDoclet来实现,而现在只要使用Annotation就可以了,但是Annotation只是一个必要条件,如果真要达到自动生成其他文件的目的,除了Annotation之外,还需要其他类,或者工具的支持。
1. 预定义Annotation
Tiger为我们预定义了3个可以用的Annotation,分别是Override,Deprecated,SuppressWarnings。
i. Override
1. 作用:
Override的作用就是,被生命为Override的方法,必须是对父类中方法的覆盖,如果父类中没有出现该方法,或者方法没有正确覆盖父类中的方法,则编译期就会出错。
2. 语法:
@Override public void methodA(){…}
3. 例子:
父类:
/*
* Created on 2005-1-4
*
*/
package test;
/**
*
* @author cenyongh@mails.gscas.ac.cn
*/
public class Test {
public Test() {
}
public void hello() {
System.out.println(“hello”);
}
public void bye() {
System.out.println(“bye”);
}
} 阅读全文 »
作者:jock
Tags: j2se
1. Executor接口
Executor接口提供了一个类似于线程池的管理工具。用于只需要往Executor中提交Runnable对象,剩下的启动线程等工作,都会有对应的实现类来完成。ScheduledExecutorService比ExecutorService增加了,时间上的控制,即用户可以在提交的时候额外的定义该任务的启动时机,以及随后的执行间隔和延迟等。
例子:
任务:
public class ETask implements Runnable{
private int id = 0;
public ETask(int id){
this.id = id;
}
public void run(){
try{
System.out.println(id+” Start”);
Thread.sleep(1000);
System.out.println(id+” Do”);
Thread.sleep(1000);
System.out.println(id+” Exit”);
}catch(Exception e){
e.printStackTrace();
}
}
}
测试类:
public class ETest{
public static void main(String[] args){
ExecutorService executor = Executors.newFixedThreadPool(2);
for(int i=0;i<5;i++){
Runnable r = new ETask(i);
executor.execute(r);
}
executor.shutdown();
}
} 阅读全文 »
作者:jock
Tags: j2se
J2SE 5.0里面添加了新的用于提高同步性能的集合类–Queue。Queue是一个FIFO队列,她的作用主要是提高了并发访问性能,她与原来集合类当中的List的区别包括:
1. 她提供了一些新的方法,用于添加元素的offer(),用于删除元素的poll(),用于获取队首元素的peak()。这些操作与原来的add(),remove()的区别在于,原有的add(),remove()和element()当所需操作失败的时候会抛出异常,而新的offer()和poll()则不会,当队列不能容纳新元素时,offer()直接返回,当队列没有元素可取的时候,poll()操作,返回null。
例子:
public class Test{
private ArrayBlockingQueue list;
public Test(int capacity){
list = new ArrayBlockingQueue(capacity);
}
public void add(int size){
for(int i=0;i list.add(i);
output();
}
}
public void output(){
for(int i:list){
System.out.print(i+” ”);
}
}
public static void main(String[] args){
Test t = new Test(2);
t.add(3);
}
}
阅读全文 »
作者:jock
Tags: j2se
先看一个5.0当中的例子:
public class Test{
public Test(){}
public void out(String …args){
for(String s:args){
System.out.println(s);
}
}
public static void main(String[] args){
Test t = new Test();
t.out(“a”,”b”,”c”);
}
}
可以注意到,在out方法的参数表当中,我们的参数声明是…args,这就是5.0引入的Varargs概念,即允许你一次传达多个参数,同样的效果如果需要在5.0之前实现的话,我们可能需要把方法的参数列表声明为一个数组,或集合,然后在调用该方法时,把参数封装到一个数组或者集合当中。即:
public void out(String[] args){}
out(new String[]{“a”,”b”,”c”});
作者:jock
Tags: j2se
Formatted Output使得我们可以使用类C的printf方法中的格式化变量,对输出进行格式化,如
System.out.printf(“%s %5d %n”,user,total);
作者:jock
Tags: j2se
以前,当我们要使用某个类当中的某个static变量,如BorderLayout.CENTER时,我们需要进行完整的书写,但通过使用Static Import,我们可以只书写CENTER即可,即:
import static java.awt.BorderLayout.* // 导入
getContentPane().add(new JPanel(),CENTER); // 使用
Static Import的使用,使得我们在不引入类的情况下,能只引用某个类的static变量
作者:jock
Tags: j2se
从5.0开始,j2se支持枚举类型。简单的枚举使用如下:
public enum Color{RED,BLUE,GREEN;};
public static void main(String[] args){
for(Color c:Color.values()){
System.out.println(c);
}
}
输出为
RED
BLUE
GREEN
稍微复杂一点的使用,可以增加对枚举类型的定义:
public enum Color{RED(1),BLUE(5),GREEN(7);
private int value;
Color(int value){this.value = value;}
public int value(){return this.value;}
};
public static void main(String[] args){
for(Color c:Color.values()){
System.out.println(c);
System.out.println(c.value() == 1);
}
}
输出为:
RED
True
BLUE
false
GREEN
false
其中在枚举的声明当中的这部分内容:
private int value;
Color(int value){this.value = value;}
public int value(){return this.value;}
就等价与声明了一个Color类:
public class Color{
private int value;
Color(int value){this.value = value;}
public int value(){return this.value;}
}
还有就是,枚举也能用于switch结构当中,象
switch(c){
case RED:…
case BLUE:…
}
作者:jock
Tags: j2se
一般来说,当我们需要对数组和集合进行遍历时,我们需要这样做:
ArrayList list = new ArrayList();
list.add(0,1);
list.add(1,2);
for (Iterator i = list.iterator(); i.hasNext();) {
Integer value=(Integer)i.next();
}
但使用了5.0的Enhanced for Loop以后,我们的循环可以变得很简单:
ArrayList list = new ArrayList();
list.add(0,1);
list.add(1,2);
for(int i:list){
System.out.println(i);
}
同理,数组的遍历也从原来的:
int[] b = new int[3];
for(int i=0;i);}
变为:
int[] b = new int[3];
for(int i:b){System.out.println(i);}
总结:
Enhanced for Loop的语法:
for ( FormalParameter : Expression )
Statement
等价于原来的:
for ( Iterator #i = Expression.iterator(); #i.hasNext(); ) {
FormalParameter = #i.next();
Statement
}
即 FormalParameter = Expression.iteraotor().next();
作者:jock
Tags: j2se
由于集合类(Collection)里面只能保存从Object当中继承而来的对象,所以对于基本类型(Primitive Type)的变量,如int,float等,在添加到Collection当中时,需要使用他们对应的封装类Integer,Float等,如:
ArrayList list = new ArrayList();
list.add(0, new Integer(42));
int total = (list.get(0)).intValue();
在5.0当中,通过使用Autoboxing和Auto-Unboxing我们就不再需要显式的使用这些封装类了。
ArrayList list = new ArrayList();
list.add(0, 42);
int total = list.get(0);
作者:jock
Tags: j2se
近期评论