Java API —— JDK5新特性

JDK5新特性
        自动拆装箱、泛型、增强for、静态导入、可变参数、枚举
 
1、增强for概述        
  1)简化数组和Collection集合的遍历        
  2)格式:            
  for(元素数据类型 变量 : 数组或者Collection集合) {         
    使用变量即可,该变量就是元素             
  }
  3)好处:简化遍历
  4)注意事项:增强for的目标要判断是否为null
例子1:
package jdk5;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by gao on 15-12-16.
 */
/*
 * 增强for:是for循环的一种。
 *
 * 格式:
 *         for(元素数据类型 变量 : 数组或者Collection集合) {
 *            使用变量即可,该变量就是元素
 *       }
 *
 * 好处:简化了数组和集合的遍历。
 *
 * 弊端: 增强for的目标不能为null。
 * 如何解决呢?对增强for的目标先进行不为null的判断,然后在使用。
 */
public class ForDemo01 {
    public static void main(String[] args) {
        // 定义一个int数组
        int[] arr = {1, 2, 3, 4, 5};
        for (int x = 0; x < arr.length; x++) {
            System.out.println(arr[x]);
        }
        System.out.println("-------------");
        //增强for
        for (int x : arr) {
            System.out.println(x);
        }
        System.out.println("-------------");
        // 定义一个字符串数组
        String[] strArray = {"林青霞", "风清扬", "东方不败", "刘意"};
        for (String s : strArray) {
            System.out.println(s);
        }
        System.out.println("-------------");
        // 定义一个集合
        ArrayList<String> array = new ArrayList<String>();
        array.add("hello");
        array.add("wrold");
        array.add("java");
        for (String s : array) {
            System.out.println(s);
        }
        List<String> list = null;
        // NullPointerException
        // 这个s是我们从list里面获取出来的,在获取前,它肯定还好做一个判断
        // 说白了,这就是迭代器的功能
        if (list != null){
            for(String s : list){
                System.out.println(s);
            }
        }
        // 增强for其实是用来替代迭代器的
        //ConcurrentModificationException
        // for (String s : array) {
        // if ("world".equals(s)) {
        // array.add("javaee");
        // }
        // }
        // System.out.println("array:" + array);
    }
}

例子2:

package jdk5;
import java.util.ArrayList;
import java.util.Iterator;
/**
 * Created by gao on 15-12-16.
 */
/*
 * ArrayList存储字符串并遍历。要求加入泛型,并用增强for遍历。
 * A:迭代器
 * B:普通for
 * C:增强for
 */
public class ForDemo02 {
    public static void main(String[] args) {
        // 创建集合对象
        ArrayList<String> arrayList = new ArrayList<String>();
        // 创建并添加元素
        arrayList.add("hello");
        arrayList.add("world");
        arrayList.add("java");
        // 遍历集合
        // 迭代器
        Iterator<String> it = arrayList.iterator();
        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);
        }
        System.out.println("------------------------");
        // 普通for
        for(int x = 0; x < arrayList.size(); x++){
            String s = arrayList.get(x);
            System.out.println(s);
        }
        System.out.println("------------------------");
        //增强for
        for(String s : arrayList){
            System.out.println(s);
        }
    }
}

例子3:

学生类:
package jdk5;
/**
 * Created by gao on 15-12-9.
 */
public class Student {
    private String name;
    private int age;
    public Student() {
    }
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

测试类:

package jdk5;
import java.util.ArrayList;
import java.util.Iterator;
/**
 * Created by gao on 15-12-16.
 */
public class ForDemo03 {
    public static void main(String[] args) {
        // 创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();
        // 创建学生对象
        Student s1 = new Student("林青霞", 27);
        Student s2 = new Student("貂蝉", 22);
        Student s3 = new Student("杨玉环", 24);
        Student s4 = new Student("西施", 21);
        Student s5 = new Student("王昭君", 23);
        // 把学生对象添加到集合中
        array.add(s1);
        array.add(s2);
        array.add(s3);
        array.add(s4);
        array.add(s5);
        // 迭代器
        Iterator<Student> it = array.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getName() + "---" + s.getAge());
        }
        System.out.println("--------------");
        // 普通for
        for (int x = 0; x < array.size(); x++) {
            Student s = array.get(x);
            System.out.println(s.getName() + "---" + s.getAge());
        }
        System.out.println("--------------");
        //增强for
        for (Student s : array) {
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}
2、静态导入(开发不常用)
  1)静态导入概述
        格式:import static 包名….类名.方法名;
        可以直接导入到方法的级别
  2)注意事项
        方法必须是静态的
        如果有多个同名的静态方法,容易不知道使用谁?这个时候要使用,必须加前缀。
由此可见,意义不大,所以一般不用,但是要能看懂。
例子1:
package jdk5;
/**
 * Created by gao on 15-12-16.
 */
//import java.lang.Math;
import static java.lang.Math.abs;
import static java.lang.Math.pow;
import static java.lang.Math.max;
public class StaticImportDemo01 {
    public static void main(String[] args) {
        // System.out.println(java.lang.Math.abs(-100));
        // System.out.println(java.lang.Math.pow(2, 3));
        // System.out.println(java.lang.Math.max(20, 30));
        // 太复杂,我们就引入到import
//        System.out.println(Math.abs(-100));
//        System.out.println(Math.pow(2, 3));
//        System.out.println(Math.max(20, 30));
        // 太复杂,有更简单
        //System.out.println(abs(-100));
        System.out.println(java.lang.Math.abs(-100));
        System.out.println(pow(2, 3));
        System.out.println(max(20, 30));
    }
    public static void abs(String s) {
        System.out.println(s);
    }
}
3、可变参数
  1)可变参数概述
        定义方法的时候不知道该定义多少个参数、
  2)格式
        修饰符 返回值类型 方法名(数据类型…  变量名){}
  3)注意:
            这里的变量其实是一个数组
            如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
  4)Arrays工具类中的一个方法
        public static <T> List<T> asList(T... a)
例子1:
package jdk5;
/**
 * Created by gao on 15-12-16.
 */
/*
 * 可变参数:定义方法的时候不知道该定义多少个参数
 * 格式:
 *         修饰符 返回值类型 方法名(数据类型…  变量名){
 *
 *         }
 *
 *         注意:
 *             这里的变量其实是一个数组
 *             如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
 */
public class ArgsDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int result = sum(a, b);
        System.out.println("result:" + result);
        // 3个数据的求和
        int c = 30;
        result = sum(a, b, c);
        System.out.println("result:" + result);
        // 4个数据的求和
        int d = 30;
        result = sum(a, b, c, d);
        System.out.println("result:" + result);
        // 需求:我要写一个求和的功能,到底是几个数据求和呢,我不太清楚,但是我知道在调用的时候我肯定就知道了
        // 为了解决这个问题,Java就提供了一个东西:可变参数
        result = sum(a, b, c, d, 40);
        System.out.println("result:" + result);
        result = sum(a, b, c, d, 40, 50);
        System.out.println("result:" + result);
    }
    private static int sum(int... a) {
        int s = 0;
        for (int x : a) {
            s += x;
        }
        return s;
    }
    // public static int sum(int a, int b, int c, int d) {
    // return a + b + c + d;
    // }
    //
    // public static int sum(int a, int b, int c) {
    // return a + b + c;
    // }
    //
    // public static int sum(int a, int b) {
    // return a + b;
    // }
}

输出结果:

result:30
result:60
result:90
result:130
result:180
 

例子2:

package jdk5;
import java.util.Arrays;
import java.util.List;
/**
 * Created by gao on 15-12-16.
 */
/*
 * public static <T> List<T> asList(T... a):把数组转成集合
 *
 * 注意事项:
 *         虽然可以把数组转成集合,但是集合的长度不能改变。
 */
public class ArgsDemo02 {
    public static void main(String[] args) {
        // 定义一个数组
//        String[] strArray = { "hello", "world", "java" };
        //固定参数
//        List<String> list = Arrays.asList(strArray);
        //可变参数
        List<String> list = Arrays.asList("hello","java");
        // UnsupportedOperationException
        // list.add("javaee");
        // UnsupportedOperationException
        // list.remove(1);
        list.set(1, "javaee");
        for(String s : list){
            System.out.println(s);
        }
    }
}