java基础10:集合框架,迭代器,泛型
集合继承关系图
a:ArrayList的继承关系:
查看ArrayList类发现它继承了抽象类AbstractList同时实现接口List,而List接口又继承了 Collection接口。Collection接口为最顶层集合接口了。
源代码:
interface List extends Collection {
}
public class ArrayList extends AbstractList implements List{
}
b:集合继承体系
这说明我们在使用ArrayList类时,该类已经把所有抽象方法进行了重写。那么,实现Collection接口的所有子类都会进行方法重写。
Collecton接口常用的子接口有:List接口、Set接口
List接口常用的子类有:ArrayList类、LinkedList类
Set接口常用的子类有:HashSet类、LinkedHashSet类
Collection 接口
|
----------------------------------------------------------------
| |
List接口 Set接口
| |
---------------- -------------
| | | |
ArrayList类 LinkedList类 HashSet类 LinkedHashSet类
2,不同集合遍历的统一方法:迭代器遍历
###迭代器的概述
A:迭代器概述:
a:java中提供了很多个集合,它们在存储元素时,采用的存储方式不同。
我们要取出这些集合中的元素,可通过一种通用的获取方式来完成。
b:Collection集合元素的通用获取方式:在取元素之前先要判断集合中有没有元素,
如果有,就把这个元素取出来,继续在判断,如果还有就再取出出来。一直把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。
c:每种集合的底层的数据结构不同,例如ArrayList是数组,LinkedList底层是链表,但是无论使用那种集合,我们都会有判断是否有元素
以及取出里面的元素的动作,那么Java为我们提供一个迭代器定义了统一的判断元素和取元素的方法
###迭代器的实现原理
*A:迭代器的实现原理
/*
* 集合中的迭代器:
* 获取集合中元素方式
* 接口 Iterator : 两个抽象方法
* boolean hasNext() 判断集合中还有没有可以被取出的元素,如果有返回true
* next() 取出集合中的下一个元素
*
* Iterator接口,找实现类.
* Collection接口定义方法
* Iterator iterator()
* ArrayList 重写方法 iterator(),返回了Iterator接口的实现类的对象
* 使用ArrayList集合的对象
* Iterator it =array.iterator(),运行结果就是Iterator接口的实现类的对象
* it是接口的实现类对象,调用方法 hasNext 和 next 集合元素迭代
*/
###迭代器的代码实现
*A:迭代器的代码实现
public class IteratorDemo {
public static void main(String[] args) {
Collection<String> coll = new ArrayList<String>();
coll.add("abc1");
coll.add("abc2");
coll.add("abc3");
coll.add("abc4");
//迭代器,对集合ArrayList中的元素进行取出
//调用集合的方法iterator()获取出,Iterator接口的实现类的对象
Iterator<String> it = coll.iterator();
//接口实现类对象,调用方法hasNext()判断集合中是否有元素
//boolean b = it.hasNext();
//System.out.println(b);
//接口的实现类对象,调用方法next()取出集合中的元素
//String s = it.next();
//System.out.println(s);
//迭代是反复内容,使用循环实现,循环的条件,集合中没元素, hasNext()返回了false
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
}
###迭代器的执行过程
A:迭代器的执行过程
a:迭代器的原理:
while(it.hasNext()) {
System.out.println(it.next());
}
//cursor记录的索引值不等于集合的长度返回true,否则返回false
public boolean hasNext() {
return cursor != size; //cursor初值为0
}
//next()方法作用:
//①返回cursor指向的当前元素
//②cursor++
public Object next() {
int i = cursor;
cursor = i + 1;
return elementData[lastRet = i];
}
b:for循环迭代写法:
for (Iterator<String> it2 = coll.iterator(); it2.hasNext(); ) {
System.out.println(it2.next());
}
###增强for循环遍历数组集合
*A:增强for循环遍历数组
a:格式:
/*
* JDK1.5新特性,增强for循环
* JDK1.5版本后,出现新的接口 java.lang.Iterable
* Collection开是继承Iterable
* Iterable作用,实现增强for循环
*
* 格式:
* for( 数据类型 变量名 : 数组或者集合 ){
* sop(变量);
* }
*/
public static void function_1(){
//for对于对象数组遍历的时候,能否调用对象的方法呢
String[] str = {"abc","itcast","cn"};
for(String s : str){
System.out.println(s.length());
}
}
/*
* 实现for循环,遍历数组
* 好处: 代码少了,方便对容器遍历
* 弊端: 没有索引,不能操作容器里面的元素
*/
public static void function(){
int[] arr = {3,1,9,0};
for(int i : arr){
System.out.println(i+1);
}
System.out.println(arr[0]);
}
###增强for循环遍历集合
A:增强for循环遍历集合
/*
* 增强for循环遍历集合
* 存储自定义Person类型
*/
public static void function_2(){
ArrayList<Person> array = new ArrayList<Person>();
array.add(new Person("a",20));
array.add(new Person("b",10));
for(Person p : array){
System.out.println(p);// System.out.println(p.toString());
}
}
泛型的通配符
public class GenericDemo {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
HashSet<Integer> set = new HashSet<Integer>();
array.add("123");
array.add("456");
set.add(789);
set.add(890);
iterator(array);
iterator(set);
}
/*
* 定义方法,可以同时迭代2个集合
* 参数: 怎么实现 , 不能写ArrayList,也不能写HashSet
* 参数: 共同实现的接口Collection
* 泛型的通配,匹配所有的数据类型 ?
*/
public static void iterator(Collection<?> coll){
Iterator<?> it = coll.iterator();
while(it.hasNext()){
//it.next()获取的对象,什么类型
System.out.println(it.next());
}
}
}
泛型的限制
/*
* 将的酒店员工,厨师,服务员,经理,分别存储到3个集合中
* 定义方法,可以同时遍历3集合,遍历三个集合的同时,可以调用工作方法
*/
import java.util.ArrayList;
import java.util.Iterator;
public class GenericTest {
public static void main(String[] args) {
//创建3个集合对象
ArrayList<ChuShi> cs = new ArrayList<ChuShi>();
ArrayList<FuWuYuan> fwy = new ArrayList<FuWuYuan>();
ArrayList<JingLi> jl = new ArrayList<JingLi>();
//每个集合存储自己的元素
cs.add(new ChuShi("张三", "后厨001"));
cs.add(new ChuShi("李四", "后厨002"));
fwy.add(new FuWuYuan("翠花", "服务部001"));
fwy.add(new FuWuYuan("酸菜", "服务部002"));
jl.add(new JingLi("小名", "董事会001", 123456789.32));
jl.add(new JingLi("小强", "董事会002", 123456789.33));
// ArrayList<String> arrayString = new ArrayList<String>();
iterator(jl);
iterator(fwy);
iterator(cs);
}
/*
* 定义方法,可以同时遍历3集合,遍历三个集合的同时,可以调用工作方法 work
* ? 通配符,迭代器it.next()方法取出来的是Object类型,就不能调用work方法
* 强制转换: it.next()=Object o ==> Employee,那么当传入的是String类型,由于不是 Employee 子类,不能调用work方法
* 方法参数总结: 控制,可以传递Employee对象,也可以传递Employee的子类的对象
* 泛型的限定 本案例,父类固定Employee,但是子类可以无限?
* ? extends Employee 限制的是父类, 上限限定, 可以传递Employee,传递他的子类对象
* ? super Employee 限制的是子类, 下限限定, 可以传递Employee,传递他的父类对象
*/
public static void iterator(ArrayList<? extends Employee> array){
Iterator<? extends Employee> it = array.iterator();
while(it.hasNext()){
//获取出的next() 数据类型,是什么Employee
Employee e = it.next();
e.work();
}
}
}