*a:集合,集合是java中提供的一种容器(存储对象的地址),可以用来存储多个数据。
*b:集合和数组的区别:
数组的长度是固定的。集合的长度是可变的。 集合中存储的元素必须是引用类型数据
02ArrayList集合回顾
*a.ArrayList集合存储5个int类型元素
public class ArrayListDemo_1{
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 class ArrayListDemo_2 {
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);
}
}
}
class Person {
String name;
Person(){
}
Person(String name){
this.name = name;
}
}
03集合的继承关系
Collection接口为最顶层集合接口,实现Collection接口的子类都会重写该接口的所有抽象方法。
*a:Collecton接口常用的子接口有:List接口、Set接口
*b: List接口常用的子类有:ArrayList类、LinkedList类
*c: Set接口常用的子类有:HashSet类、LinkedHashSet类
04集合Collection的方法
/* * Collection接口中的方法是集合中所有实现类必须拥有的方法 */ public class CollectionDemo { public static void main(String[] args) { function(); function_1(); function_2(); function_3(); } /* * Object[] toArray() 集合中的元素,转成一个数组中的元素, 集合转成数组 * 返回一个存储对象的数组, 数组存储的数据类型是Object */ private static void function_2() { Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("123"); coll.add("def"); coll.add("456"); coll.add("ghi"); Object[] objs = coll.toArray(); for(int i = 0 ; i < objs.length ; i++){//数组遍历 System.out.println(objs[i]); } } /* * boolean contains(Object o) 判断对象是否存在于集合中,对象存在返回true * 方法参数是Object类型 */ private static void function_1() { Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("123"); coll.add("def"); coll.add("456"); coll.add("ghi"); boolean b = coll.contains("123"); System.out.println(b);//true } /* * void clear() 清空集合中的所有元素(集合容器本身依然存在) */ public static void function(){ //接口多态的方式调用 Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("bcd"); System.out.println(coll);//[abc,bcd] coll.clear(); System.out.println(coll);//[] } /* * boolean remove(Object o)移除集合中指定的元素 */ private static void function_3(){ Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("123"); coll.add("def"); coll.add("456"); coll.add("ghi"); coll.add("789"); System.out.println(coll);//[abc,123,def,456,ghi,789] boolean b = coll.remove("def"); System.out.println(b);//true System.out.println(coll);//[abc,123,456,ghi,789] } }
05迭代器
*A:迭代器概述: *a:java中提供了很多个集合,在存储元素时,采用的存储方式不同。若要取出这些集合中的元素,可通过一种通用的获取方式来完成(迭代器)。 *b:Collection集合元素的通用获取方式:在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续判断,如果还有就再取出来。一直把集合中的所有元素全部取出,这种取出方式称为迭代。 *c:每种集合的底层的数据结构不同,Java提供一个迭代器定义了统一的判断元素和取元素的方法,例如ArrayList是数组,LinkedList底层是链表,但是无论使用那种集合,都会有判断是否有元素hasNext(),取出里面的元素next()。 *B:迭代器实现原理: *a:Iterator接口获得实现类的方法: Collection接口定义了抽象方法Iterator iterator(); ArrayList 重写抽象方法 iterator(),返回Iterator接口的实现类的对象 *b:接口 Iterator 的两个抽象方法: boolean hasNext() 判断集合中还有没有可以被取出的元素,如果有返回true next() 取出集合中的下一个元素 例: ArrayList<String> array = new ArrayList<String>(); Iterator it =array.iterator();//运行结果就是Iterator接口的实现类的对象 it是接口的实现类对象,调用方法 hasNext()和 next() 完成集合元素迭代
06迭代器的代码实现
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");
//调用集合的方法iterator()获得Iterator接口的实现类的对象
Iterator<String> it = coll.iterator();
//使用循环实现迭代
while(it.hasNext()){
String s = it.next();//接口的实现类对象it调用方法next()取出集合中的元素
System.out.println(s);
}
}