class Temp{
String id ="";
public Temp() {
super();
}
public Temp(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
public class JdkTest {
// jdk1.5泛性、For-Each循环,特性实例
static List
List
for (Temp emp : c) {
strs.add(emp.getId());
}
return strs;
}
//jdk1.5可变参数(Varargs),特性实例
public static void write(String... objs) {
for (String obj: objs){
System.out.print(obj);
}
System.out.print("\n");
}
public static void main(String args[]){
List
c.add(new Temp("1"));
c.add(new Temp("2"));
c.add(new Temp("3"));
List
for(String str: strs){
System.out.println(str);
}
write("hello");
write("hello"," world");
write("hello"," world"," welcome");
write("hello"," world"," welcome"," to");
write("hello"," world"," welcome"," to"," Leaderbird");
}
}
泛型(Generic):
//类型安全,不用转型
List
//说明class A是范型类,T可以是任意类型
//多个类型参数的时候,用逗号分开
//接口用法一样
class A
private T t;
public void setT(T t){
this.t = t;
}
}
public class Test{
public static void main(String[] args){
A
a.setT("str") //规范了class A里的T类型,所以只能传递String进去
}
}
//受限范型
public class Limited
public static void main(String[] args) {
Limited
Limited
}
}
//通配符
public void print(List> list){ // 如写成 List
// ..
}
public void draw2All(List extends Shape> shapes){ //可以是任何Shape的子类
// ..
}
--------------------------------------------------
public class Test {
public static void main(String[] args) throws Exception {
// 可换成Integer[] a = {1,2,3},但List的类型也必须由String换成Integer: List
String[] a = {"1","2","3"};
List
System.out.println(list.size());
fromArrayToCollection(a,list); // fromArrayToCollection方法强制了参数的类型
System.out.println(list.size());
}
public static
for (T o : a)
c.add(o);
}
}
For-Each循环:
Object[] os = new Object[5];
for(Object o : os){
// ..
}
//和"泛型"配合的话:
List
for(String s : list){ // 可以直接告诉程序,遍历后的类型是 String
// ..
}
自动装包:基本类型自动转为包装类.(int >> Integer)
自动拆包:包装类自动转为基本类型.(Integer >> int)
//一般,jdk1.5降级到1.4,多少会产生点"自动装包/拆包问题"
枚举(Enums):
public enum Color
{
Red,
White,
Blue
}
// Color myColor = Color.Red;
// 提供了两个有用的静态方法values()和valueOf()
for (Color c : Color.values())
System.out.println(c);
例子:
--------------------------------------------------
package demo.test.jdk15;
import java.util.*;
public class EnumTest {
public enum Authority {
READ, READ_MAIL, WRITE, EXECUTE;
}
public enum Role {
USER() {
private Authority[] au = { Authority.EXECUTE };
Authority[] getAuthorities() {
return au;
}
},
MANAGER() {
private Authority[] au = { Authority.READ, Authority.EXECUTE };
Authority[] getAuthorities() {
return au;
}
},
SUPER() {
private Authority[] au = { Authority.WRITE, Authority.READ,
Authority.EXECUTE, Authority.READ_MAIL };
Authority[] getAuthorities() {
return au;
}
};
abstract Authority[] getAuthorities();
}
public enum User {
MARY() {
Role[] r = { Role.USER };
Role[] getRole() {
return r;
}
},
JACK() {
Role[] r = { Role.MANAGER , Role.USER };
Role[] getRole() {
return r;
}
};
abstract Role[] getRole();
}
public static void main(final String[] args) {
Map
map.put("user", User.JACK);
Authority[] needAu = { Authority.READ, Authority.EXECUTE };
if (hasAuthority((User) map.get("user"), needAu)) {
System.out.println("+");
} else {
System.out.println("-");
}
}
public static boolean hasAuthority(User u, Authority... a) {
int needAu = a.length;
if (needAu == 0) {
return false;
}
Role[] roles = u.getRole();
int hasAu = 0;
for (Authority i : a) {
for (Role r : roles) {
boolean find = false;
for (Authority j : r.getAuthorities()) {
System.out.println(r + " -- " + i + ":" + j);
if (j.ordinal() == i.ordinal()) {
if(needAu == ++hasAu){
return true;
}
find = true;
break;
}
}
if(find){
break;
}
}
}
return false;
}
}
--------------------------------------------------
util.write(obj1);
util.write(obj1,obj2);
util.write(obj1,obj2,obj3);
// 为了解决此类问题(如多态问题),可用"可变参数"
public void write(Object...objs){
// ..
}
// 注:传入的参数(如果有)会被当作一个数组处理,如要知道参数的数量,可取length
静态导入(Static Imports)
// "静态导入"会降低程序可读性,如无特殊需求,不建议使用
import static java.lang.Math.*; //是Math.* 不是Math
double d = sin(PI * 2); //无需再写 Math.sin(Math.PI * 2);
java.util.Scanner(扫描器)
//读取Console的内容
Scanner sc = new Scanner(System.in);
int i = sc.nextInt(); //可以是 nextXX()
//正则分割(类似Strint.split(xx))
String input = "a xx b xx c xx d xx";
Scanner sc = new Scanner(input).useDelimiter("\\s*xx\\s*");
while(sc.hasNext()){
System.out.println(sc.next());
}
printf 和 java.util.Formatter:
//类似C里面的printf,喜欢的人可以用
//printf(Locale l, String format, Object... args)
//printf(String format, Object... args)
System.out.printf("%08d %s \n",5,"ss"); // 00000005 ss (换行)
//和 out.format(format, args) 效果相同
//还可以用 Formatter
String s = new Formatter().format("%08d %s \n", 5,"ss").toString();
System.out.println(s);
Properties (loadFromXML storeToXML)(用Properties读写xml)
//test.xml:
//注:
//loadFromXML
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("e:/test.xml");
prop.loadFromXML(fis);
fis.close();
System.out.println("Name: " + prop.getProperty("name")); // 这里的name是entry的key
//storeToXML
Properties prop = new Properties();
prop.setProperty("id", "2");
prop.setProperty("name", "aho");
prop.setProperty("email", "[email protected]");
FileOutputStream fos = new FileOutputStream("e:/test.xml");
prop.storeToXML(fos, "这是注释","utf-8");
fos.close();
另外:
BigDecimal 多了很多实用方法,算钱方便多了~
Double 和 Float 提供了toHexString()方法(以前Integer/Long有)
Interger 位操作的扩充函数
System.clearProperty(String key) 清除一个属性(从前有getProperty()有setProperty(),但没有remove)
Double/Float/Long/Integer/Short/Byte等,都加入了一个静态常量 SIZE ,如Double.SIZE = 64