/**
* 自定义集合接口, 类似java.util.Collection
* 用于数据存储
* @author stone
*
*/
public interface ICollection<T> {
IIterator<T> iterator(); //返回迭代器
void add(T t);
T get(int index);
}
/**
* 自定义迭代器接口 类似于java.util.Iterator
* 用于遍历集合类ICollection的数据
* @author stone
*
*/
public interface IIterator<T> {
boolean hasNext();
boolean hasPrevious();
T next();
T previous();
}
/**
* 集合类, 依赖于MyIterator
* @author stone
*/
public class MyCollection<T> implements ICollection<T> {
private T[] arys;
private int index = -1;
private int capacity = 5;
public MyCollection() {
this.arys = (T[]) new Object[capacity];
}
@Override
public IIterator<T> iterator() {
return new MyIterator<T>(this);
}
@Override
public void add(T t) {
index++;
if (index == capacity) {
capacity *= 2;
this.arys = Arrays.copyOf(arys, capacity);
}
this.arys[index] = t;
}
@Override
public T get(int index) {
return this.arys[index];
}
}
/*
* 若有新的存储结构,可new 一个ICollection, 对应的 new 一个IIterator来实现它的遍历
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class Test {
public static void main(String[] args) {
ICollection<Integer> collection = new MyCollection<Integer>();
add(collection, 3, 5, 8, 12, 3, 3, 5);
for (IIterator<Integer> iterator = collection.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
System.out.println("-------------");
ICollection collection2 = new MyCollection();
add(collection2, "a", "b", "c", 3, 8, 12, 3, 5);
for (IIterator iterator = collection2.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
}
static <T> void add(ICollection<T> c, T ...a) {
for (T i : a) {
c.add(i);
}
}
}
3 5 8 12 3 3 5 ------------- a b c 3 8 12 3 5
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有