(32)Java学习笔记——集合框架 / Collection 接口 / Iterator 迭代器

来源:互联网 发布:中欧基金 知乎 编辑:程序博客网 时间:2024/06/05 04:50

集合:

存储多个对象的容器类型的变量。集合类提供了变化长度的需求。


集合和数组的不同点:

A/ 长度区别:

------| 数组长度固定

------|集合长度可变

B/ 内容不同

------|数组存储的是同意类型的元素

------|集合可以存储不同类型的元素

C/元素的数据类型

------|数组可以存储基本数据类型,也可以存储引用数据类型

------|集合只能存储引用类型


Collection 

是集合的顶层接口,表示一组对象,这些对象称为Collection的元素。它的子体系有重复的,有唯一的,有有序的,有无序的。

功能概述:

1:添加功能

              boolean add(Object obj)   添加一个元素

              boolean addAll(Collection c)   添加一个集合元素

2:删除功能

               void clear()      移除所有元素

               boolean remove()   移除一个元素

               boolean removeAll( )      移除多个元素   (移除一个元素就是移除)

3:判断功能

               boolean contains(Object obj)    判断集合中是否包含指定的元素

               boolean containsAll(Collection c)    判断集合中是否包含制定的集合元素  (只有包含所有的元素才是包含)

               boolean isEmpty()  判断集合是否为空

4:获取功能

               Iterator<E> iterator()   迭代器:集合的专用遍历方式

5:长度功能

               int size()   元素个数

6:交集功能

               boolean retainAll(Collection c)    两个集合都有的元素  (变化的是谁调用这个方法,元素就被谁保存了)

               //假设有两个集合,A对B做交集,最终的结果保存在A中,B保持不变

               //返回值表示A中是否发生过变化

7:把集合转换为数组

               Object[ ]  toArray()


范例:不带All的方法

package cn.itcast_01;import java.util.ArrayList;import java.util.Collection;public class CollectionDemo {public static void main(String[] args) {//测试不带All的方法//创建集合对象    //接口不能实例化,需要找可实现的子接口的实现类Collection c = new ArrayList();    //boolean add(Object obj)   添加一个元素//System.out.println("c:"+c.add("hello"));c.add("hello");c.add("world");c.add("java");System.out.println(c);   //查看操作结果  //返回结果[hello, world, java]/*//void clear()  移除所有元素c.clear();System.out.println(c);   //查看操作结果   //返回结果[ ]*//*//boolean remove()   移除一个元素c.remove("hello");System.out.println(c);   //查看操作结果 //返回结果[world, java]*///boolean contains(Object obj)    判断集合中是否包含指定的元素System.out.println("contains:"+c.contains("hello"));//contains:trueSystem.out.println(c);   //查看操作结果  //返回结果//boolean isEmpty()  判断集合是否为空System.out.println("isEmpty:"+c.isEmpty());//isEmpty:falseSystem.out.println(c);   //查看操作结果  //返回结果// int size()   元素个数int result = c.size();System.out.println(result); //3}}

范例02:带All的方法

package cn.itcast_01;import java.util.ArrayList;import java.util.Collection;public class CllectionDemo2 {public static void main(String[] args) {//测试带All的方法//创建集合1Collection c1 = new ArrayList();c1.add("abc1");c1.add("abc2");c1.add("abc3");c1.add("abc4");//创建集合2Collection c2 = new ArrayList();c2.add("abc4");c2.add("abc5");c2.add("abc6");c2.add("abc7");System.out.println("c1:"+c1);System.out.println("c2:"+c2);/*// boolean addAll(Collection c)   添加一个集合元素System.out.println("addAll:"+c1.addAll(c2));System.out.println("c1:"+c1);System.out.println("c2:"+c2);*//*// boolean removeAll( )      移除多个元素System.out.println("remove:"+c1.removeAll(c2));System.out.println("c1:"+c1);System.out.println("c2:"+c2);*///boolean containsAll(Collection c)    判断集合中是否包含制定的集合元素System.out.println("containsAll:"+c1.containsAll(c2));System.out.println("c1:"+c1);System.out.println("c2:"+c2);//boolean retainAll(Collection c)    两个集合都有的元素   //假设System.out.println("retainAll:"+c1.retainAll(c2));//c1:[abc4]   c2:[abc4, abc5, abc6, abc7]System.out.println("c1:"+c1);System.out.println("c2:"+c2);}}
范例03:遍历集合

               Object[ ]  toArray()

package cn.itcast_01;import java.util.ArrayList;import java.util.Collection;/* * 集合的遍历.其实就是依次获取集合中的元素 * Object[ ]  toArray() :把集合转换为数组,可以实现集合的遍历 *  */public class CollectionDemo3 {public static void main(String[] args) {//创建集合对象Collection c= new ArrayList();//添加元素c.add("hello");  // 实际是是Object obj = "hello"; 向上转型c.add("world");c.add("java");//遍历// Object[ ]  toArray() :把集合转换为数组Object[] objs = c.toArray();for(int x=0;x<objs.length;x++){//System.out.println(objs[x]);//我知道元素是字符串,需求:在获取元素的同时,还想知道元素的长度//因为Object中没有length()方法。要想使用字符串的方法length(),就必须吧元素还原成字符串——向下转型String s = (String) objs[x];System.out.println(s+"----"+s.length());}}}
练习01:拥挤和存储5个学生对象,并把学生对象遍历

学生对象:

package cn.itcast_02;public class Student {private String name;private int age;public Student() {super();// TODO Auto-generated constructor stub}public Student(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

测试程序:

package cn.itcast_02;import java.util.ArrayList;import java.util.Collection;/* * 练习:用集合存储5个学生对象,并把学生对象遍历 * 思路: * A:创建学生类 * B:创建集合对象 * C:创建学生对象 * D:把学生添加到集合 * E:把集合转成数组 * F:遍历数组 */public class CollectionTest_01 {public static void main(String[] args) {//创建集合对象Collection c = new ArrayList();//创建学生对象Student s1 = new Student("李刚",30);Student s2 = new Student("姚晨",28);Student s3 = new Student("金城武",45);Student s4 = new Student("冯小刚",60);Student s5 = new Student("郭富城",50);//把学生对象添加到集合c.add(s1);c.add(s2);c.add(s3);c.add(s4);c.add(s5);//把集合转成数组Object[] objs = c.toArray();//遍历数组for(int x = 0;x<objs.length;x++){Student s = (Student) objs[x];//向下转型System.out.println(s.getName()+"----"+s.getAge());}}}


迭代器:

Iterator<E> iterator()  迭代器:集合的专用遍历方式,它是一个接口。迭代器是依赖于集合存在的

Object next();    获取元素,并移动到下一个位置

boolean hasNext() :判断是否有下一个元素可以迭代。

范例:

package cn.itcast_03;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/* * 迭代器:集合的专用遍历方式 */public class IteratorDemo {public static void main(String[] args) {//创建集合对象Collection c = new ArrayList();c.add("hello");c.add("world");c.add("java");//Iterator iterator() 迭代器:集合的专用遍历方式Iterator it = c.iterator();  //实际返回的是子类对象 (多态)//Object obj = it.next();/*System.out.println(it.next());  //helloSystem.out.println(it.next());//worldSystem.out.println(it.next());//java*///我们应该在每次获取钱,进行一个判断。判断是否有下一个元素,有就获取,没有就不进行while(it.hasNext()){String s = (String) it.next();System.out.println(s);}}}


练习02:拥挤和存储5个学生对象,并把学生对象遍历,用迭代器遍历(接练习01学生类)

package cn.itcast_02;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/* * 练习:用集合存储5个学生对象,并吧学生对象进行遍历,用迭代器遍历 */public class IteratorTest {public static void main(String[] args) {//创建集合对象Collection c = new ArrayList();//创建学生对象Student s1 = new Student("李刚",30);Student s2 = new Student("姚晨",28);Student s3 = new Student("金城武",45);Student s4 = new Student("冯小刚",60);Student s5 = new Student("郭富城",50);//把学生对象添加到集合c.add(s1);c.add(s2);c.add(s3);c.add(s4);c.add(s5);//遍历  用迭代器便利Iterator it = c.iterator();while(it.hasNext()){Student s = (Student) it.next();  //向下转型System.out.println(s.getName()+"----"+s.getAge());}}}

集合的使用步骤:

A:创建集合对象

B:创建元素对象

C:把元素添加到集合

D:遍历集合

               a: 通过集合对象获取迭代器对象

               b: 通过迭代器对象的hasNext()方法判断是否有元素

               c: 通过迭代器对象的next()方法获取元素并移动到下一个位置



0 0
原创粉丝点击