import java.util.List;
import java.util.Vector;
import java.util.ArrayList;
import java.util.LinkedList;
public class TestBasicGeneric {
public static void main(String[] args) {
//数组:元素的类型是一致
Integer[] nums = new Integer[10];
Object[] objs = new Object[10];
//集合:元素的类型可以不一致
List list = new ArrayList();
list.add(new Student("tom",18));
list.add(new Student("jack",20));
//list.add(100);//Object o = 100;/Integer.valueOf(100);
//list.add("abc");
//list.add(12345);
//list.add(true);
for(int i = 0 ; i < list.size() ; i++) {
//访问学生的各个属性
Student s = list.get(i);
System.out.println(s.name +"\t"+s.age);
}
List numbers = new ArrayList();// = Integer
List Vector = new Vector();
List linkeds = new LinkedList();
}
}
class Student{
String name;
int age;
public Student(String name , int age) {
this.name = name;
this.age = age;
}
}
运行结果:
tom 18
jack 20
泛型:高级类别的知识,熟练应用,需要时间、经验的积累import java.util.Iterator;
public class TestInstanceGeneric {
public static void main(String[] args) {
MyClass mc2 = new MyClass();
MyClass mc3 = new MyClass();
mc3.println(3.5);
mc2.println(50);
}
}
/*
* 案例1(类的实例泛型)
* */
class MyClass{//E代表一种通配,可以是任意类型,未指明类型前:为Object
public void m1(E e) {//泛型可以动态
}
public void m2(Object o) {//固定写死,不能变
}
public void println(E e) {
//逻辑代码都一样
}
// public void println(Integer i) {
// //逻辑代码都一样
// }
// public void println(String s) {
// //逻辑代码都一样
// }
}
/*
* 案例2(类的实例泛型)
* */
//E = Element / T = Type / K = Kye / V = Value
interface MyInterface{//实例泛型
public T method(T t);
}
class MyLmplClass implements MyInterface{
public Dog2 method(Dog2 t) {
return null;
}
}
class MyLmplClass2 implements MyInterface{
public Cat2 method(Cat2 t) {
return null;
}
}
class Dog2{}
class Cat2{}
interface Comparable2{//可比较,可排序
public int compareTo(E obj);//Object参数的通用,还是在具体业务场景中稍显麻烦
}
class MyStudent implements Comparable2{
int age;
@Override
public int compareTo(MyStudent obj) {
if(this.age > obj.age) {
return -1;
}
return 0;
}
}
class MyTeacher implements Comparable2{
@Override
public int compareTo(MyTeacher obj) {
return 0;
}
public void showInfo(T t) {
}
}
class MyItClass implements Iterator{
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public MyStudent next() {
// TODO Auto-generated method stub
return null;
}
}
静态泛型:import java.util.ArrayList;
import java.util.List;
public class TestStaticGeneric {
public static void main(String[] args) {
List list1 = new ArrayList();//约束,集合中可以存储的对象
List list2 = new ArrayList();
List list3 = new ArrayList();
//List list4 = new ArrayList();//不建议,类型不安全,不一致
List list5 = new ArrayList();
List list6 = new ArrayList();
List list7 = new ArrayList();
m1(list1);
//m1(list2);//error
//m1(list3);//error
//m1(list4);//error
//m1(list5);//error
//m1(list6);//error
m1(list7);
}
//?代表任意泛型
//可接受的是所有的动物集合
/**
* ? extends Animal 泛型类型必须是Animal的子类
* ? extends Comparable 泛型类型必须是Comparable的实现类
* ? super Dog 泛型类型必须是Dog或Dog父类
*/
public static void m1(List list) {
//省略
}
}
class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
class Bird extends Animal implements Comparable{
@Override
public int compareTo(Bird obj) {
return 0;
}
}
class Bus implements Comparable{
@Override
public int compareTo(Bus obj) {
return 0;
}
}
class Bicycle{}
泛型参数,约定好规则用以接收相关类型的参数;
import java.io.Serializable;
public class TestStaticGeneric2 {
public static void main(String[] args) {
m1(100);//Integer
m1(12.34);//Double
m1(true);
m1("abc");
m2(56);
m3(new MyNumber());
m4(100);
}
public static void m1(Object t) {//宽泛(特别宽泛,不存在约束了)
}
public static void m2(T t) {
//此时T的含义是Object
}
public static void m3(T t) {
//此时T的含义是Number类或者Number的子类
}
public static void m4(T t) {
//此时T必须是Number类或者Number的子类的同时也要是Comparable接口的实现类
//父类只有一个,必须写在最前面
}
}
class MyNumber extends Number{
@Override
public int intValue() {
// TODO Auto-generated method stub
return 0;
}
@Override
public long longValue() {
// TODO Auto-generated method stub
return 0;
}
@Override
public float floatValue() {
// TODO Auto-generated method stub
return 0;
}
@Override
public double doubleValue() {
// TODO Auto-generated method stub
return 0;
}
}
向中添加规则来约束泛型的接收范围,约束泛型接收的必须是某个类或其子类;
import java.util.ArrayList;
import java.util.List;
public class TestStaticGeneric3 {
public static void main(String[] args) {
m1(new ArrayList());
m1(new ArrayList());
m1(new ArrayList());
m2(new ArrayList());
m2( new ArrayList() );
m3( new ArrayList());
m3( new ArrayList());
}
public static void m1(List list) {
//此时T代表Object
}
//可以为Comparable约定泛型
public static <T extends Comparable> void m2(List list) {
//此时T代表Comparable接口的实现类,且必须是String泛型
}
public static <T extends Comparable> void m3(List list) {
//此时T代表Comparable接口的实现类,且必须是本身类型泛型
//完成:集合中的所有对象,必须具备本类型的两个元素进行比较
//当List被传入实参后,要求T所代表的类型,必须实现Comparable接口,同时,接口泛型必须是T类型
}
}
class MyClass2 implements Comparable{//和String没有比较的必要,不实际
@Override
public int compareTo(String o) {
// TODO Auto-generated method stub
return 0;
}
}
class MyClass3 implements Comparable{
@Override
public int compareTo(MyClass3 o) {
// TODO Auto-generated method stub
return 0;
}
}
可以用泛型来约束接收的类型必须是某个接口的实现类
import java.util.ArrayList;
import java.util.List;
public class TestStaticGeneric4 {
public static void main(String[] args) {
List students = new ArrayList();
students.add(new Student2(20));
students.add(new Student2(18));
students.add(new Student2(22));
List teachers = new ArrayList();
teachers.add(new Teacher(20));
teachers.add(new Teacher(18));
teachers.add(new Teacher(22));
m(students);
m(teachers);
List persons = new ArrayList();//Student Teacher
persons.add( new Student2(16) );
persons.add( new Student2(20) );
persons.add( new Teacher(30) );
persons.add( new Teacher(27) );
m(persons);
java.util.Collections.sort(students);
for(int i = 0 ; i < students.size() ; i++) {
System.out.println(students.get(i) +"\t" + students.get(i).age);
}
System.out.println();
java.util.Collections.sort(teachers);
for(int i = 0 ; i < teachers.size() ; i++) {
System.out.println(teachers.get(i) +"\t" + teachers.get(i).age);
}
System.out.println();
java.util.Collections.sort(persons);
for(int i = 0 ; i < persons.size() ; i++) {
System.out.println(persons.get(i) + "\t" + persons.get(i).age);
}
}
/*
* 原案例:当List被传入实参后,要求T所代表的类型,必须实现Comparable接口,同时,接口泛型必须是T类型
*
* 1.如果要求Comparable必须是自身类型,则导致,无法对一组父类引用的对象进行排序,
* 2.故而加入 实现Comparable接口时,无论是父类还是子类,都可以成为泛型参数
* */
public static <T extends Comparable> void m(List list) {
}
}
class Person implements Comparable{
int age;
public Person(int age) {
this.age = age;
}
@Override
public int compareTo(Person o) {
if(this.age o.age){
return 1;
}
return 0;
}
}
class Student2 extends Person{
public Student2(int age) {
super(age);
}
}
class Teacher extends Person{
public Teacher(int age) {
super(age);
}
}
运行结果:
com.qf.Day24.Student2@7852e922 18
com.qf.Day24.Student2@4e25154f 20
com.qf.Day24.Student2@70dea4e 22
com.qf.Day24.Teacher@5c647e05 18
com.qf.Day24.Teacher@33909752 20
com.qf.Day24.Teacher@55f96302 22
com.qf.Day24.Student2@3d4eac69 16
com.qf.Day24.Student2@42a57993 20
com.qf.Day24.Teacher@75b84c92 27
com.qf.Day24.Teacher@6bc7c054 30
通过泛型约束,避免非法类型的输入;
12.3.5 Colletions工具类 概念:集合工具类 ,定义了除了存取以外的集合常用方法; 方法:import java.util.Arrays;
import java.util.List;
public class TestCollectionsTool {
public static void main(String[] args) {
List numbers = Arrays.asList(1,2,5,6,4,3,7,9,8);
java.util.Collections.sort(numbers);//升序排序
for(int i = 0 ; i < numbers.size() ; i++) {
System.out.print(numbers.get(i).toString());
}
System.out.println();
java.util.Collections.reverse(numbers);//反转顺序
for(int i = 0 ; i < numbers.size() ; i++) {
System.out.print(numbers.get(i).toString());
}
System.out.println();
java.util.Collections.shuffle(numbers);//随机重置顺序
for(int i = 0 ; i < numbers.size() ; i++) {
System.out.print(numbers.get(i).toString());
}
}
}
输出结果:
123456789
987654321
351246879
通过Collections类可对集合进行灵活操作,可从API上查找方法的具体功能;