您的位置:首页 > 编程语言 > Java开发

Java API ——Collection集合类 & Iterator接口

2015-12-14 15:52 639 查看
对象数组举例:

学生类:

package itcast01;
/**
* Created by gao on 15-12-9.
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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 itcast01;
/**
* Created by gao on 15-12-9.
*/
/*
* 我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
*         学生:Student
*         成员变量:name,age
*         构造方法:无参,带参
*         成员方法:getXxx()/setXxx()
*         存储学生的数组?自己想想应该是什么样子的?
* 分析:
*         A:创建学生类。
*         B:创建学生数组(对象数组)。
*         C:创建5个学生对象,并赋值。
*         D:把C步骤的元素,放到数组中。
*         E:遍历学生数组。
*/
public class ObjectArrayDemo01 {
public static void main(String[] args) {
// 创建学生数组(对象数组)。
Student[] students = new Student[5];
// 创建5个学生对象,并赋值。
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("刘意", 30);
Student s4 = new Student("赵雅芝", 60);
Student s5 = new Student("王力宏", 35);
// 把C步骤的元素,放到数组中。
students[0] = s1;
students[1] = s2;
students[2] = s3;
students[3] = s4;
students[4] = s5;
// 遍历
for (int x = 0; x < students.length; x++) {
//System.out.println(students[x]);//没有重写toString方法,打印地址值
Student s = students[x];
System.out.println(s.getName()+"---"+s.getAge());
}
}
}


输出结果:

林青霞---27
风清扬---30
刘意---30
赵雅芝---60
王力宏---35

对象数组的内存图解:



1、API - 集合类



1) 集合类概述

· 为什么出现集合类?
面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。
· 数组和集合类同是容器,有何不同?
数组虽然也可以存储对象,但长度是固定的;集合长度是可变的。数组中可以存储基本数据类型,集合只能存储对象。
· 集合类的特点
集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。
· 集合的由来:

我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储。而要想存储多个对象,就不能是一个基本的变量,而应该是一个容器类型的变量,在我们目前所学过的知识里面,有哪些是容器类型的呢?数组和StringBuffer。但是呢?StringBuffer的结果是一个字符串,不一定满足我们的要求,所以我们只能选择数组,这就是对象数组。而对象数组又不能适应变化的需求,因为数组的长度是固定的,这个时候,为了适应变化的需求,Java就提供了集合类供我们使用。
· 数组和集合的区别?
A:长度区别

        数组的长度固定
        集合长度可变
B:内容不同
        数组存储的是同一种类型的元素
        而集合可以存储不同类型的元素
      C:元素的数据类型问题
        数组可以存储基本数据类型,也可以存储引用数据类型
        集合只能存储引用类型

刚说过集合是存储多个元素的,但是呢,存储多个元素我们也是有不同需求的:比如说,我要这多个元素中不能有相同的元素,再比如说,我要这多个元素按照某种规则排序一下。针对不同的需求,Java就提供了不同的集合类,这样呢,Java就提供了很多个集合类。这多个集合类的数据结构不同,结构不同不重要的,重要的是你要能够存储东西,并且还要能够使用这些东西,比如说判断,获取等。既然这样,那么,这多个集合类是有共性的内容的,我们把这些集合类的共性内容不断的向上提取,最终就能形成集合的继承体系结构。
数据结构:数据的存储方式。

· 集合的继承体系图解:



2、 Collection 集合类:

  是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。



3、Collection的功能概述:
A:添加功能
boolean add(Object obj):添加一个元素
boolean addAll(Collection c):添加一个集合的元素(添加所有)
B:删除功能
void clear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):移除一个集合的元素(只有包含一个就行)
C:判断功能
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
boolean isEmpty():判断集合是否为空(元素为空)
D:获取功能
Iterator<E> iterator()(重点)
E:长度功能
int size():元素的个数
面试题:数组有没有length()方法呢?(有)字符串有没有length()方法呢?(有)集合有没有length()方法呢?(没有)
F:交集功能
boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
G:把集合转换为数组
Object[] toArray()

测试类1:(不带All的方法)

package collectiondemo;
import java.util.ArrayList;
import java.util.Collection;
/* Collection的功能概述:
* 1:添加功能
*         boolean add(Object obj):添加一个元素
*         boolean addAll(Collection c):添加一个集合的元素
* 2:删除功能
*         void clear():移除所有元素
*         boolean remove(Object o):移除一个元素
*         boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
* 3:判断功能
*         boolean contains(Object o):判断集合中是否包含指定的元素
*         boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
*         boolean isEmpty():判断集合是否为空
* 4:获取功能
*         Iterator<E> iterator()(重点)
* 5:长度功能
*         int size():元素的个数
*         面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
* 6:交集功能
*         boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
* 7:把集合转换为数组
*         Object[] toArray()
*/
public class CollectionDemo01 {
public static void main(String[] args) {
// 测试不带All的方法
//创建集合对象
// Collection c = new Collection(); //错误,因为接口不能实例化
Collection c = new ArrayList(); //可有重复
// boolean add(Object obj):添加一个元素
// System.out.println("add:"+c.add("hello"));
c.add("hello");
c.add("world");
c.add("java");
// void clear():移除所有元素
//c.clear();
// boolean remove(Object o):移除一个元素
//        System.out.println("remove:"+c.remove("hello")); //remove:true
//        System.out.println("remove:"+c.remove("javaee")); //remove:false
// boolean contains(Object o):判断集合中是否包含指定的元素
//        System.out.println("contains:"+c.contains("hello")); //contains:true
//        System.out.println("contains:"+c.contains("android")); //contains:false
// boolean isEmpty():判断集合是否为空
System.out.println("isEmpty:"+c.isEmpty()); //isEmpty:false
//int size():元素的个数
System.out.println("size:"+c.size()); //size:3
System.out.println("c:"+c); //c:[hello, world, java]
}
}


测试类2:(带All的方法)

package collectiondemo;
import java.util.ArrayList;
import java.util.Collection;
/**
* Created by gao on 15-12-9.
*/
public class CollectionDemo02 {
public static void main(String[] args) {
// 创建集合1
Collection c1 = new ArrayList();
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c1.add("abc4");
// 创建集合2
Collection c2 = new ArrayList();
c2.add("abc1");
c2.add("abc2");
c2.add("abc3");
c2.add("abc4");
c2.add("abc5");
c2.add("abc6");
c2.add("abc7");
// boolean addAll(Collection c):添加一个集合的元素(添加c2集合中的所有元素,可重复)
//        System.out.println("addAll:"+c1.addAll(c2)); //addAll:true
//        System.out.println("c1:"+c1); //c1:[abc1, abc2, abc3, abc4, abc4, abc5, abc6, abc7]
//        System.out.println("c2:"+c2); //c2:[abc4, abc5, abc6, abc7]
//boolean removeAll(Collection c):移除一个集合的元素(只要移除c中的一个元素就算成功)
//        System.out.println("removeAll:"+c1.removeAll(c2)); //removeAll:true
//        System.out.println("c1:"+c1); //c1:[abc1, abc2, abc3]
//        System.out.println("c2:"+c2); //c2:[abc4, abc5, abc6, abc7]
//boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(只有包含c的所有元素才算成功)
//        System.out.println("containsAll:"+c1.containsAll(c2)); //containsAll:false
//        System.out.println("c1:"+c1); //c1:[abc1, abc2, abc3, abc4]
//        System.out.println("c2:"+c2); //c2:[abc4, abc5, abc6, abc7]
//boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
//假设有两个集合A,B。
//A对B做交集,最终的结果保存在A中,B不变。
//返回值表示的是A是否发生过改变。
//        System.out.println("retainAll:"+c1.retainAll(c2)); //retainAll:true;c1发生了改变
//        System.out.println("c1:"+c1); //c1:[abc4]
//        System.out.println("c2:"+c2); //c2:[abc4, abc5, abc6, abc7]
System.out.println("retainAll:"+c1.retainAll(c2)); //retainAll:false;c1没有发生改变
System.out.println("c1:"+c1); //c1:[abc1, abc2, abc3, abc4]
System.out.println("c2:"+c2); //c2:[abc1, abc2, abc3, abc4, abc5, abc6, abc7]
}
}


测试类3:普通遍历

package collectiondemo;
import java.util.ArrayList;
import java.util.Collection;
/**
* Created by gao on 15-12-9.
*/
/*
* 集合的遍历。其实就是依次获取集合中的每一个元素。
*
* Object[] toArray():把集合转成数组,可以实现集合的遍历
*/
public class CollectionDemo03 {
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]);
// 我知道元素是字符串,我在获取到元素的的同时,还想知道元素的长度。
// System.out.println(objs[x] + "---" + objs[x].length());
// 上面的实现不了,原因是Object中没有length()方法
// 我们要想使用字符串的方法,就必须把元素还原成字符串
// 向下转型
String s = (String) objs[x];
System.out.println(s + "---"+s.length());
}
}
}


遍历[b]应用:[/b]
Student类:

package collectiondemo02;
/**
* Created by gao on 15-12-9.
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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 collectiondemo02;
import java.util.ArrayList;
import java.util.Collection;
/**
* Created by gao on 15-12-9.
*/
public class StudentDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
//把学生添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 把集合转成数组
Object[] objects = c.toArray();
//遍历数组
for(int x = 0; x < objects.length; x++){
Student s = (Student) objects[x];
System.out.println(s.getName()+"-->"+s.getAge());
}
}
}


4、Iterator接口Iterator

1)Iterator接口概述
  对 collection 进行迭代的迭代器
  依赖于集合而存在
2) Iterator接口成员方法

boolean hasNext()
E next()



package collectiondemo03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Created by gao on 15-12-14.
*/
/*
* Iterator iterator():迭代器,集合的专用遍历方式
*         Object next():获取元素,并移动到下一个位置。
*             NoSuchElementException:没有这样的元素,因为你已经找到最后了。
*         boolean hasNext():如果仍有元素可以迭代,则返回 true。(
*/
public class CollectionDemo01 {
public static void main(String[] args) {
Collection c = new ArrayList();
// 创建并添加元素
// String s = "hello";
// c.add(s);
c.add("hello");
c.add("world");
c.add("java");
// Iterator iterator():迭代器,集合的专用遍历方式
Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态
// 判断是否有下一个元素,有就获取,没有就不搭理它
while(it.hasNext()){
String s = (String) it.next();
System.out.println(s);
}
}
}


输出结果:

hello
world
java

测试类1:Iterator
引入学生类Student,
测试类:

package collectiondemo03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Created by gao on 15-12-14.
*/
/*
* 练习:用集合存储5个学生对象,并把学生对象进行遍历。用迭代器遍历。
*
* 注意:
*         A:自己的类名不要和我们学习的要使用的API中的类名相同。
*         B:复制代码的时候,很容易把那个类所在的包也导入过来,容易出现不能理解的问题。
*/
/*
* 问题1:能用while循环写这个程序,我能不能用for循环呢?
* 问题2:不要多次使用it.next()方法,因为每次使用都是访问一个对象。
*/
public class CollectionTest01 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
// 把学生添加到集合中
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());
}
//for改写,不过我们习惯用while
//        for (Iterator it = c.iterator(); it.hasNext(); ) {
//            Student s = (Student) it.next();
//            System.out.println(s.getName() + "---" + s.getAge());
//        }
}
}


输出结果:

Student{name='林青霞', age=27}
Student{name='风清扬', age=30}
Student{name='令狐冲', age=33}
Student{name='武鑫', age=25}
Student{name='刘晓曲', age=22}

5、集合遍历Iterator类总结:



迭代器的源码结构:

public interface Inteator {
boolean hasNext();
Object next();
}
public interface Iterable {
Iterator iterator();
}
public interface Collection extends Iterable {
Iterator iterator();
}
public interface List extends Collection {
Iterator iterator();
}
public class ArrayList implements List {
public Iterator iterator() {
return new Itr();
}

private class Itr implements Iterator {
public boolean hasNext() {}
public Object next(){}
}
}
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Iterator it = c.iterator();     //new Itr();
while(it.hasNext()) {
String s = (String)it.next();
System.out.println(s);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: