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

Java集合框架 入门

2015-08-12 00:00 591 查看
摘要: Java复习 入门学习

一、Java集合概述

1、Java集合就像一种容器,我们可以把多个对象扔进该容器中。同样数组也可以用来保存对象,数组跟集合的区别是,

①、数组的长度是不可变的(在内存中是一片地址连续的内存单元),如果需要保存数量变化的数据,数组就有点无能为力了。集合的底层也是有数组,链表,树实现的。

②、数组无法保存具有映射关系的数据。

③、数组可以保存基本类型数据,也可以保存对象。而集合只能保存对象(保存对象的引用变量)。

④、数组保存的数据类型要求只能是一种类型的。

2、Java集合类主要有两个接口派生出的:Collection和Map接口,这两个接口是Java集合框架的根接口。Collection接口又包含一些子接口:List、Set、Queue接口。在Collection接口中定义了一些共性方法,这里不罗嗦了。

3、Iterator迭代器:

Iterator接口也是Java集合框架的成员,但是它与Collection系列、Map系列的集合是不一样的。Iterator则主要用于遍历Collection集合中的元素。

集合中的每个容器的数据结构都是不一样的,所以每个容器的取出遍历的细节也是不同的,所以Java集合的取(遍历)方式是定义在集合内部的,那么这样的方式就被定义成了内部类。所有的容器都有取出这个动作,属于共性内容,所以将其共性提取,为Iterator接口。那么如何获取集合的取出对象呢?是通过iterator()方法。

Collection接口继承了一个Iterable,Iterable接口中定义了iterator方法。

public interface Iterable<T> {
Iterator<T> iterator();
}

Collection接口定义:

public interface Collection<E> extends Iterable<E>

Iterator接口,定义规则:只有三个方法,每个容器内部都自己有不同的实现方式。

public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}

在AbstractList类中定义了Itr内部类,实现了Iterator接口:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {

private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;

/**
* Index of element returned by most recent call to next or
* previous.  Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;

/**
* The modCount value that the iterator believes that the backing
* List should have.  If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;

public boolean hasNext() {
return cursor != size();
}

public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}

public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();

try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}

final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}

private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
cursor = index;
}

public boolean hasPrevious() {
return cursor != 0;
}

public E previous() {
checkForComodification();
try {
int i = cursor - 1;
E previous = get(i);
lastRet = cursor = i;
return previous;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}

public int nextIndex() {
return cursor;
}

public int previousIndex() {
return cursor-1;
}

public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();

try {
AbstractList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}

public void add(E e) {
checkForComodification();

try {
int i = cursor;
AbstractList.this.add(i, e);
lastRet = -1;
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
}

当使用Iterator迭代访问Collection集合元素时,Collection集合里的元素不能被改变,只有通过Iterator的remove方法删除上一次next方法返回的集合元素才可以。否则将会引发异常。Iterator迭代器采用的是快速失败机制(fail-fast),一旦在迭代过程中检测到该集合已经被修改程序也将包异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: