java容器(一) Collection

来源:互联网 发布:皮卡堂百万秘籍软件 编辑:程序博客网 时间:2024/05/14 21:32

Collection是所有序列容器的根接口。java.util.AbstractCollection类提供了Collection的默认实现。

Collection的定义语句

public interface Collection<E> extends Iterable<E> {

iterable的实现

public interface Iterable<T> {    /**     * Returns an iterator over a set of elements of type T.     *     * @return an Iterator.     */    Iterator<T> iterator();}

说明实现Collection必须实现iterator方法


下面是一个实现Iterable的类

import java.util.Iterator;public class IterableImp implements Iterable<String>{String[] str= new String[8];public Iterator<String> iterator(){return new Iterator<String>(){private int index = 0;public boolean hasNext(){return index < str.length;}public String next(){return str[index++];}public void remove() {}}; }}



0 0
原创粉丝点击