Java语言学习day35--8月10日

今日内容介绍
1、集合
2、Iterator迭代器
3、增强for循环
4、泛型



 

 

 


###01集合使用的回顾
*A:集合使用的回顾
*a.ArrayList集合存储5个int类型元素
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(111);
list.add(222);
list.add(333);
list.add(444);
list.add(555);
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
}

*b.ArrayList集合存储5个Person类型元素
public static void main(String[] args) {
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person(“小强”));
list.add(new Person(“老王”));
list.add(new Person(“小虎”));
list.add(new Person(“小泽”));
list.add(new Person(“小红”));
for(int i=0; i<list.size(); i++){
Person p = list.get(i);
System.out.println(p);
}
}


###02集合的学习目标
集合,集合是java中提供的一种容器,可以用来存储多个数据。
在前面的学习中,我们知道数据多了,可以使用数组存放或者使用ArrayList集合进行存放数据。那么,集合和数组既然都是容器,它们有啥区别呢?
 数组的长度是固定的。集合的长度是可变的。
 集合中存储的元素必须是引用类型数据

###03集合继承关系图
A:集合继承关系图
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类

###04集合Collection的方法
A:集合Collection的方法
/*
* Collection接口中的方法
* 是集合中所有实现类必须拥有的方法
* 使用Collection接口的实现类,程序的演示
* ArrayList implements List
* List extends Collection
* 方法的执行,都是实现的重写
*/
public class CollectionDemo {
public static void main(String[] args) {
function_2();
}


/* Collection接口方法
* Object[] toArray() 集合中的元素,转成一个数组中的元素, 集合转成数组
* 返回是一个存储对象的数组, 数组存储的数据类型是Object
*/
private static void function_2() {
Collection<String> coll = new ArrayList<String>();
coll.add("abc");
coll.add("itcast");
coll.add("itheima");
coll.add("money");
coll.add("123");

Object[] objs = coll.toArray();
for(int i = 0 ; i < objs.length ; i++){
System.out.println(objs[i]);
}
}
/*
* 学习Java中三种长度表现形式
* 数组.length 属性 返回值 int
* 字符串.length() 方法,返回值int
* 集合.size()方法, 返回值int
*/

/*
* Collection接口方法
* boolean contains(Object o) 判断对象是否存在于集合中,对象存在返回true
* 方法参数是Object类型
*/
private static void function_1() {
Collection<String> coll = new ArrayList<String>();
coll.add("abc");
coll.add("itcast");
coll.add("itheima");
coll.add("money");
coll.add("123");

boolean b = coll.contains("itcast");
System.out.println(b);
}


/*
* Collection接口的方法
* void clear() 清空集合中的所有元素
* 集合容器本身依然存在
*/
public static void function(){
//接口多态的方式调用
Collection<String> coll = new ArrayList<String>();
coll.add("abc");
coll.add("bcd");
System.out.println(coll);

coll.clear();

System.out.println(coll);

}
}

###05集合Collection的remove方法
A:05集合Collection的remove方法
/*
* Collection接口方法
* boolean remove(Object o)移除集合中指定的元素
*/
private static void function_3(){
Collection<String> coll = new ArrayList<String>();
coll.add("abc");
coll.add("money");
coll.add("itcast");
coll.add("itheima");
coll.add("money");
coll.add("123");
System.out.println(coll);

boolean b = coll.remove("money");
System.out.println(b);
System.out.println(coll);
}

###06迭代器的概述
A:迭代器概述:
a:java中提供了很多个集合,它们在存储元素时,采用的存储方式不同。
我们要取出这些集合中的元素,可通过一种通用的获取方式来完成。

b:Collection集合元素的通用获取方式:在取元素之前先要判断集合中有没有元素,
如果有,就把这个元素取出来,继续在判断,如果还有就再取出出来。一直把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。

c:每种集合的底层的数据结构不同,例如ArrayList是数组,LinkedList底层是链表,但是无论使用那种集合,我们都会有判断是否有元素
以及取出里面的元素的动作,那么Java为我们提供一个迭代器定义了统一的判断元素和取元素的方法

###07迭代器的实现原理
*A:迭代器的实现原理
/*
* 集合中的迭代器:
* 获取集合中元素方式
* 接口 Iterator : 两个抽象方法
* boolean hasNext() 判断集合中还有没有可以被取出的元素,如果有返回true
* next() 取出集合中的下一个元素
*
* Iterator接口,找实现类.
* Collection接口定义方法
* Iterator iterator()
* ArrayList 重写方法 iterator(),返回了Iterator接口的实现类的对象
* 使用ArrayList集合的对象
* Iterator it =array.iterator(),运行结果就是Iterator接口的实现类的对象
* it是接口的实现类对象,调用方法 hasNext 和 next 集合元素迭代
*/

###08迭代器的代码实现
*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);
}



}
}
###09迭代器的执行过程
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());
}

posted @ 2021-08-10 18:03  open520  阅读(33)  评论(0)    收藏  举报