您的位置:首页 > 编程语言 > Java开发

【JavaSE学习笔记】泛型,jdk5之后新特性

2017-08-02 20:32 429 查看
泛型,jdk5之后新特性

A.泛型

1)概述

创建集合对象或者去调用方法的时候,将数据类型(引用类型)当作一种参数进行传递

2)格式

<数据类型>:引用类型

3)特点

a.将运行时期的异常提前到了编译期间

b.不用再强制转换类型

c.解决了黄色警告线的问题

注意:在sun公司,jdk官方文档中,凡是类后面,接口后面,抽象类后面带有<E>:泛型定义

import java.util.ArrayList;
import java.util.Iterator;

public class Demo01 {
public static void main(String[] args) {
// 创建ArrayList集合对象
ArrayList<String> al = new ArrayList<String>();// ----------->jdk7以后的特性:叫泛型推断,

// 给集合中添加元素
al.add("hello");
al.add("world");
al.add("java");

// 给集合中添加一个Integer类型的元素
// array.add(new Integer(100)) ;

// 将运行时期异常提前到编译期间
// array.add(100);//没有报错的原因是什么呢?自动装箱:int--->Integer类型

// 获取迭代器对象
Iterator<String> i = al.iterator();
while (i.hasNext()) {
String s = i.next();
System.out.println(s);
}

}
}

4)使用泛型来存储自定义对象,并且遍历该对象的成员!

首先建一个学生类,跟之前一样,含有name,age成员变量,在此不再介绍,直接调用

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class Demo02 {
public static void main(String[] args) {
// 创建集合对象
ArrayList<Student> als = new ArrayList<Student>();// 前后保持一致

//创建4个学生对象
Student s1 = new Student("Tom", 20);
Student s2 = new Student("Tim", 21);
Student s3 = new Student("Tommon", 22);
Student s4 = new Student("Timmon", 23);

//给集合中添加对象
als.add(s1);
als.add(s2);
als.add(s3);
als.add(s4);

//方式1 普通for循环
for (int i = 0; i < als.size(); i++) {
Student s = als.get(i);
System.out.println(s);
}
System.out.println("--------------------------------");

//方式2 Iterator iterator()
Iterator<Student> its = als.iterator();
while (its.hasNext()) {
Student s = its.next();
System.out.println(s);
}
System.out.println("--------------------------------");

//方式3 列表迭代器
ListIterator<Student> lls = als.listIterator();
while (lls.hasNext()) {
Student s = lls.next();
System.out.println(s);
}
}
}

5)泛型的应用场景

定义在类(具体类或者抽象类),接口的后面,一般情况下,在集合中使用居多

接口

public class ObjectTool<T> {// T代表所有类
private T obj;

public T getObj() {
return obj;
}

public void setObj(T obj) {
this.obj = obj;
}

}//测试类
//测试类
public class Demo03 {
public static void main(String[] args) {
// 创建工具类对象:已经将数据类型给定了
ObjectTool<String> ots = new ObjectTool<String>();

// ots.setObj(new Integer(29));使用泛型提高了程序的安全性
ots.setObj(new String("Tom"));
String s = ots.getObj();
System.out.println("姓名是:" + s);

ObjectTool<Integer> oti = new ObjectTool<Integer>();
oti.setObj(new Integer(27));
Integer i = oti.getObj();
System.out.println("年龄是:" + i);
}
}

6)泛型可以定义在方法上吗?

public class ObjectTool {
public <T> void show(T t) {// 泛型定义在方法上:格式:public <T> void 方法名 (重点)
System.out.println(t);
}
}给类后面没有添加泛型也可以操作,需要泛型定义在方法上
//测试类
public class Demo03 {
public static void main(String[] args) {
ObjectTool ot = new ObjectTool();
ot.show("hello");
ot.show(true);
ot.show(100);
}
}


7)将泛型定义在接口上

接口(也可以把泛型定义在抽象方法上:abstract后面。。自行测试)

//接口:将泛型定义在接口上
public interface Inter<T> {
public abstract void show(T t);
}
子实现类

//第二种情况:不明确数据类型不明确接口的数据类型是什么
public class InterImpl<T> implements Inter<T> {

@Override
public void show(T t) {
System.out.println(t);
}
}
测试类

//测试类
public class Demo04 {
public static void main(String[] args) {
Inter<String> is = new InterImpl<String>();
is.show("Tom");

Inter<Integer> ii = new InterImpl<Integer>();
ii.show(29);
}
}

8)泛型高级:通配符

?:代表任意数据类型:可以是Object类型以及Java类

? extends E:向下限定,要么跟当前类型一样,要么是它的子类

? super E:向上限定,保持当前类型一致,或者是它的父类

import java.util.ArrayList;
import java.util.Collection;

class Animal{}

class Dog extends Animal{}

class Cat extends Animal{}

public class Demo05 {
public static void main(String[] args) {
Collection<Object> c1 = new ArrayList<Object>();
Collection<Animal> c2 = new ArrayList<Animal>();

//?
Collection<?> c3 = new ArrayList<Object>();
Collection<?> c4 = new ArrayList<Animal>();
Collection<?> c5 = new ArrayList<Dog>();
Collection<?> c6 = new ArrayList<Cat>();

//? extends E
Collection<? extends Object> c7 = new ArrayList<Animal>();
Collection<? extends Animal> c8 = new ArrayList<Animal>();
Collection<? extends Animal> c9 = new ArrayList<Cat>();
Collection<? extends Animal> c10 = new ArrayList<Dog>();

//? super E
Collection<? super Animal> c11 = new ArrayList<Object>();
Collection<? super Cat> c12 = new ArrayList<Animal>();
}
}

9)集合的嵌套

我们有一个班里,班里有很多学生,遍历学生的信息

我们隔壁班也有学生----->ArrayList<Student>.有很多班,最终的大的集合

ArrayList<ArrayList<Student>>------->自定义学生类

然后实现集合的嵌套遍历(增强for循环,B节介绍):使用ArrayList

import java.util.ArrayList;

public class Demo06 {
public static void main(String[] args) {
// 定义一个大集合对象
ArrayList<ArrayList<Student>> aals = new ArrayList<ArrayList<Student>>();

// 创建第一个班集合对象
ArrayList<Student> als1 = new ArrayList<Student>();

// 创建学生对象
Student s1 = new Student("刘备", 29);
Student s2 = new Student("关羽", 28);
Student s3 = new Student("张飞", 27);

// 给集合添加对象
als1.add(s1);
als1.add(s2);
als1.add(s3);

// 将第一个集合对象添加到大集合中
aals.add(als1);

// 创建第二个班集合对象
ArrayList<Student> als2 = new ArrayList<Student>();

// 创建学生对象
Student s4 = new Student("曹操", 29);
Student s5 = new Student("许褚", 28);
Student s6 = new Student("张辽", 27);

// 给集合添加对象
als2.add(s4);
als2.add(s5);
als2.add(s6);

// 将第二个集合对象添加到大集合中
aals.add(als2);

// 创建第三个班集合对象
ArrayList<Student> als3 = new ArrayList<Student>();

// 创建学生对象
Student s7 = new Student("孙权", 29);
Student s8 = new Student("周瑜", 28);
Student s9 = new Student("陆逊", 27);

// 给集合添加对象
als3.add(s7);
als3.add(s8);
als3.add(s9);

// 将第三个集合对象添加到大集合中
aals.add(als3);

// 使用增强for循环
for (ArrayList<Student> arrayList : aals) {
for (Student s : arrayList) {
System.out.println(s);
}
System.out.println("----------------------------");
}
}
}

B.jdk5之后新特性

1)概述

jdk5以后的新特性:自动拆装箱,泛型,枚举,增强for循环,静态导入,可变参数等等

2)增强for循环

是用来将数组和集合的遍历简单化

a.格式:(集合中用的比较多)

for(数据类型(引用类型) 变量名 : 数组或者集合的对象名称){

输出变量名;

}

b.应用场景:不是集合就是数组(int类型(建议写Integer)/String类型的居多)

c.弊端:该对象不能为空,否则报错:NullPointerException:空指针异常

d.为什么说增强for循环相当于迭代器?

上一章中,判断如果字符串元素是一个world元素

给集合中添加一个新的元素:javaee

在增强for中,会发生并发异常

3)使用增强for循环,遍历字符串

import java.util.ArrayList;

public class Demo01 {
public static void main(String[] args) {
ArrayList<String> als = new ArrayList<String>();

als.add("hello");
als.add("world");
als.add("java");

for (String s : als) {
System.out.println(s);
}
}
}

4)集合存储自定义对象并遍历

import java.util.ArrayList;

public class Demo02 {
public static void main(String[] args) {
ArrayList<Student> als = new ArrayList<Student>();

Student s1 = new Student("刘备", 29);
Student s2 = new Student("关羽", 28);
Student s3 = new Student("张飞", 27);
Student s4 = new Student("赵云", 26);

als.add(s1);
als.add(s2);
als.add(s3);
als.add(s4);

for (Student s : als) {
System.out.println(s);
}
}
}

5)静态导入

a.格式:import static java.lang.Math.abs;

b.注意:1.一类中有静态方法,就可以使用静态导入

              2.如果要使用静态导入,为了防止在一个类中出现同名的方法

                那么调用的时候需要前缀来区别开来

import static java.lang.Math.abs;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

public class Demo03 {
public static void main(String[] args) {
// System.out.println(abs(-10));
System.out.println(java.lang.Math.abs(-100));
System.out.println(pow(2, 3));
System.out.println(sqrt(4));
}

public static void abs(int i) {
System.out.println(i);
}
}

6)可变参数

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

b.多个参数其实相当于由数据组成的一个数据

c.如果一个方法中有多个参数,那么可变参数指定的最后一个参数

求数据之和

public class Demo04 {
public static void main(String[] args) {
int sum = sum(10,20,30,40,50);
System.out.println(sum);
}

public static int sum(int...a) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
}

7)数组转换成集合

Arrays数组工具类

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

注意:数组可以转换成集合,但是集合长度不能改变

import java.util.Arrays;
import java.util.List;

public class Demo05 {
public static void main(String[] args) {
String[] strArray = { "hello", "world", "java" };

List<String> asList = Arrays.asList(strArray);

// 给集合中添加一个元素
// asList.add("javaee");java.lang.UnsupportedOperationException
// asList.remove(1);

for (String s : asList) {
System.out.println(s);
}

// 修改集合中的元素
asList.set(1, "word");
System.out.println(asList);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息