JDK5以后的新特性

 Jdk5以后的新特性:

增强for,静态导入,可变参数,泛型,自动拆装箱...

泛型和自动拆装箱我们在之前已经学习过了,这节我们就来看一下其余三个特性。

一、增强for循环

增强for循环的格式
for(数据大类型  变量名 : 数组或者集合对象名){
输出变量即可!
}
 
增强for的出现是替代迭代器的,所以在遍历集合或者遍历数组就可以使用增强for去完成,在遍历集合时,一定要避免并发修改异常的出现,由于增强for是替代迭代器的,所以如果出现并发修改异常时,还是使用size()方法和get()方法相结合的方式。

增强for循环的弊端:如果集合的对象是null,如果再次对集合操作,就会出现空指针异常:NullPointerException
解决方法:对集合进行非空判断

练习:ArrayList集合存储自定义对象并遍历,有几种方式?
1)Iterator iterator() ;
2)listIterator listiterator();(基本不用)
3)普通for循环:size()/get(int index)

4)增强for循环

public class ArrayListTest {
	
	public static void main(String[] args) {
		
		//创建ArrayList集合
		ArrayList<Student> array = new ArrayList<Student>() ;
		
		//创建学生对象
		Student s1 = new Student("张三", 20) ;
		Student s2 = new Student("李四", 19) ;
		Student s3 = new Student("王五", 22) ;
		Student s4 = new Student("赵六", 26) ;
		
		//添加元素
		array.add(s1) ;
		array.add(s2) ;
		array.add(s3) ;
		array.add(s4) ;
		
		//方式1:迭代器遍历:Iterator 
		Iterator<Student> it = array.iterator() ;
		while(it.hasNext()) {
			Student s = it.next() ;
			System.out.println(s.getName()+"----"+s.getAge());
		}
		System.out.println("-------------------");
		
		//方式2:普通for
		for(int x = 0 ; x < array.size() ; x ++) {
			Student s = array.get(x) ;
			System.out.println(s.getName()+"----"+s.getAge());
		}
		System.out.println("-------------------");
		
		//方式3:增强for循环
		for(Student s : array) {
			System.out.println(s.getName()+"----"+s.getAge());
		}
	}
}

二、静态导入

格式:public static 包名.类名.方法名,静态导入是导入到方法级别,这个比较简单,我们直接写代码:

import static java.lang.Math.abs;		//导入到方法的级别
import static java.lang.Math.pow;
public class ImportStaticDemo {
	
	public static void main(String[] args) {
		
		//静态导入
		System.out.println(pow(2,3));
		//可以使用带前缀的方式
		System.out.println(java.lang.Math.abs(-100));
		
		System.out.println("-------------------------");
		
		//本身当前的某个类中的方法名和需要被静态导入的方法名一样,必须加上前缀
		//System.out.println(abs(-100));
		System.out.println(java.lang.Math.abs(-100));
		System.out.println(pow(2,3));

	}
	
	public static void abs() {
		
	}
}

三、可变参数

可变参数:当一个方法的参数个数不确定的时候,要使用可变参数

格式:
修饰符 返回值类型  方法名(数据类型...变量名){...}

注意:
1)变量名:看成一个数组
2)使用的时候数据类型...

根据具体的需求去完成,一般情况,知道有这个特性就可以了,看源码时会遇到。

需求:求数据之和

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);
		
		int c = 30 ;
		int result2 = sum(a,b,c);
		System.out.println("result2:"+result2);
		
		int d = 40 ;
		int result3 = sum(a,b,c,d);
		System.out.println("result3:"+result3);
		
		//当我们的参数不确定的情况下,就可以使用jdk5一个特性:可变参数
		int result4 = sum(a,b,c,d,50);
		System.out.println("result4:"+result4);
	}
	
	//定义一个参数不确定的方法,通过可变参数操作
	public static int sum(int...a) {
		//求和:将a是多个参数的,看成数组
		
		//先定义最终结果变量
		int result = 0 ;
		//增强for遍历可变参数a
		for(int n :a) {
			result += n ;
		}
		return result;
	}
}

四、其他

1)Array类的asList方法

针对数组操作的工具类:Arrays,提供了一个方法:

public static <T> List<T> asList(T... a) :将数组转换成固定大小的集合

注意:如果使用此方法,那么集合的长度不可变

public class ArraysDemo {
	
	public static void main(String[] args) {
		
		List<String> list = Arrays.asList("hello","world","java") ;
		
		//list.add("javaee") ;// java.lang.UnsupportedOperationException :集合固定的,长度是固定的
		//list.remove("world") ;//集合长度不能变
		//修改集合中的元素内容
		list.set(1, "javaee"); 
		//增强for
		for(String s:list) {
			System.out.println(s);
		}
	}
}
2)集合的嵌套遍历
需求:
有一个Java基础班,里面有很多学生,把一个班级集合:ArrayList<Student>

        不止一个Java基础班,这个时候就定义大集合:ArrayList<ArrayList<Student>>

        有三个基础班,分别遍历每一个班里面学生信息:(name,age) 

public class ArrayListTest {
	
	public static void main(String[] args) {
		
		//创建一个大的集合
		ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>() ;
		
		//创建第一个班的集合对象
		ArrayList<Student> firstArray = new ArrayList<Student>() ;
		//创建学生对象
		Student s1 = new Student("曹操",35) ;
		Student s2 = new Student("诸葛亮", 29);
		Student s3 = new Student("蒋干", 30) ;
		
		//给第一个班添加元素
		firstArray.add(s1) ;
		firstArray.add(s2) ;
		firstArray.add(s3) ;
		
		//将第一个集合添加到大集合中
		bigArrayList.add(firstArray) ;
		
		//创建第二个班集合对象
		ArrayList<Student> secondArray = new ArrayList<Student>() ;
		Student s11 = new Student("宋江",38) ;
		Student s22 = new Student("鲁智深", 29);
		Student s33 = new Student("武松", 30) ;
		
		secondArray.add(s11) ;
		secondArray.add(s22) ;
		secondArray.add(s33) ;
		
		//添加到大集合
		bigArrayList.add(secondArray) ;
		
		//创建第三个集合对象
		ArrayList<Student> thirdArray = new ArrayList<Student>() ;
		Student s111 = new Student("高圆圆",38) ;
		Student s222 = new Student("唐嫣", 29);
		Student s333 = new Student("刘若英", 30) ;
		
		thirdArray.add(s111) ;
		thirdArray.add(s222) ;
		thirdArray.add(s333) ;
		//添加到集合
		bigArrayList.add(thirdArray) ;
		
		//增强for遍历:大集合
		for(ArrayList<Student> array: bigArrayList) {
			for(Student s:array) {
				System.out.println(s.getName()+"---"+s.getAge());
			}
		}
	}
}
3)其他练习

需求:

求10个1~20之间的随机数,要求数据不能重复 这里不能使用set集合


分析:
可以使用数组,数组的长度固定,不够好,使用集合去做
创建一个随机数类对象
Random
获取指定的随机数的范围:
int nextInt() ;  获取的int类型的范围
int nextInt(int n) ;获取0~n之间的数据 ,[0,n)

创建一个ArrayList集合对象,元素类型:Integer类型
定义统计遍历:count 是从0开始
判断如果统计遍历小于10
先计算出1-20之间的随机数,还需要判断集合中是否包含这些元素
如果不包含,添加,count++
包含,重复,不搭理了....

大于10,不搭理

遍历即可

public class ArrayListTest {
	
	public static void main(String[] args) {
		//创建随机数类对象
		Random r = new Random() ;
		
		//创建一个ArrayList集合
		ArrayList<Integer> array = new ArrayList<Integer>() ;
		
		//定义统计变量
		int count = 0 ;
		
		//判断统计变量的个数
		while(count <10) {
			//个数符合的情况下,计算出1-20之间的随机数
			int number = r.nextInt(20) +1 ;
			
			//需要判断集合总是发包含这个number
			if(!array.contains(number)) {
				array.add(number) ;
				count ++ ;
			}
		}
		
		//增强for
		for(Integer i : array) {
			System.out.println(i);
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值