目录
1.3.2Collection 接口常用方法,以实现子类 ArrayList 来演示. CollectionMethod.java
1.4.1 Collection 接口遍历元素方式 1-使用 Iterator(迭代器)
1.4.3Collection 接口遍历对象方式 2-for 循环增强
1.1.1集合的引出
在学习集合前,通常我们储存多个数据使用的是数组,但是当我们想保存不同类型的数组时就显现了数组的不足。我们来分析一下数组都有哪些不足吧。
1.1.2数组的disadvantage
- 数组的长度必须指定
- 数组内存放的一定是同一类型的数据
- 使用数组进行增删改查时比较麻烦
1.1.3集合的advantage
- 可以动态保存多种类型的对象
- 提供了一系列操作对象的方法(add,remove,set,get)
- 集合可以动态扩容
1.2.1集合的框架结构
java的集合类有很多种,主要分为两大类(如图所示)
- Collection接口继承了Iterable接口,List接口和Set接口继承了Collection接口,Vector ArrayList LinkedList 类实现了List接口,TreeSet 类和HashSet类实现了Set接口。(Vector ArrayList LinkedList 类称为List接口的实现子类)
- Collection 接口有两个重要的子接口List Set ,他们的实现子类都是单列集合
- . Map接口的实现子类 是双列集合,存放的K-V
1.3.1Collection接口和常用方法
public interface Collection <E> extends iterable<E>
- collection的实现子类可以存放多个元素,每个元素可以是object
- 有些collection的实现类,可以存放重复的元素,有些不可以
- 有些collection的实现类是有序的(List),有些是无序的(Set)
- Collection接口没有直接实现的子类,是通过他的接口List 和 Set 来实现的
1.3.2Collection接口常用方法,以实现子类ArrayList来演示. CollectionMethod.java
package com.cds.CollectionMethod;
import java.util.ArrayList;
import java.util.List;
public class CollectionMethod {
public static void main(String[] args) {
List list = new ArrayList();
list.add("jack");//添加元素
list.add(10);//这里存在将int类型转换成Integer
System.out.println("list=" + list);
System.out.println(list.contains("jack"));//返回Boolean
System.out.println(list.size());//得到集合的大小
System.out.println(list.isEmpty());//判断是否为空
// list.remove(0);//删除第0个元素
// list.remove(jcak);//删除指定元素
// list.clear();//清空
// System.out.println(list);
ArrayList list2 = new ArrayList();
list2.add("刘小美");
list2.add(0,"张晓梅");
System.out.println("===========");
System.out.println("list2= "+list2);
list.addAll(list2);//添加多个元素
System.out.println("list="+list);
System.out.println(list.containsAll(list2));
list2.add(0,"柴东升");
System.out.println("list="+list);
System.out.println("list2="+list2);
}
}
1.4.1 Collection接口遍历元素方式1-使用Iterator(迭代器)
- Iterator对象称为迭代器,主要用于遍历Collection集合中的元素
- 所有实现了Collection接口的集合类都有一个Iterator()方法
- Iterator仅仅用于遍历集合,Iterator本身并不存放对象
1.4.2迭代器的执行原理(看代码,含详细解释)
package com.cds.collectionIterator;
import java.util.ArrayList;
import java.util.Iterator;
public class CollectionIterator01 {
public static void main(String[] args) {
@SuppressWarnings({"all"})
ArrayList arrayList = new ArrayList();
arrayList.add(new Book("平凡的世界","路遥",30.5));
arrayList.add(new Book("白鹿原","陈忠实",60.0));
arrayList.add(new Book("告白与告别","韩寒",45.5));
arrayList.add(18);
Iterator iterator = arrayList.iterator();//得到arrayList的迭代器
while(iterator.hasNext()){//hasNext()判断是否还有下一个元素
Object obj = iterator.next();//1、下移2、将 下面的集合位置上元素返回
System.out.println(obj );
}
iterator = arrayList.iterator();//重置迭代器
System.out.println(iterator.hasNext());
for (Object book:arrayList){
System.out.println("book="+book);
}
// while (iterator.hasNext()) {
// Object next = iterator.next();
//
// }
}
}
class Book{
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
在调用Iterator.next()方法前必须要调用Iterator.hashNext进行检测.如不调用且下一条数据无效,则iterator.next()会抛出如下图的异常
遍历完成后如果需要再次遍历.则需要重置遍历器(iterator = list.iterator();)
1.4.3Collection接口遍历对象方式2-for循环增强
增强for循环可以代替iterator迭代器,zengqiangfor循环就是简化版的iterator,本质一样.只能用于遍历集合或者数组
1.4.4基本语法
debug进入增强版for循环中调用的Iterator方法仍然是返回一个 iterator迭代器
底层源码
本篇博客到这里就结束了,下一篇我会主要介绍ArrayList的底层结构和源码分析
版权声明:本文为qq_64513441原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。