设计模式之Iterator(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010995220/article/details/51260389

实现动态添加对象的容器。

实现一:手动编写ArrayList

package com.awiatech.iterator;

/**
 * 一个动态添加对象的容器,底层使用数组模拟。
 * 与数组相比的好处:不用考虑数组的边界问题,可以动态扩展。
 * @author Chicago
 * 
 */
public class ArrayList {
	Object[] objects = new Object[10]; // 定义一个长度为10的数组
	int index = 0; // 数组索引指向
	
	/**
	 * 数组中添加元素
	 * @param o 要添加的元素
	 */
	public void add(Object o){
		// 当原数组数据满时再开辟两倍长度的新数组,并拷贝原数组数据至新数组中,并将原数组指向新数组
		if(index == objects.length){
			Object[] newObjects = new Object[objects.length * 2];
			System.arraycopy(objects, 0, newObjects, 0, objects.length);
			objects = newObjects;
		}
		objects[index] = o; // 元素添加到数组
		index ++; // 移动索引指向
	}
	
	/**
	 * 数组中元素的个数
	 * @return 返回数组中元素的个数
	 */
	public int size(){
		return index;
	}

}

测试:

package com.awiatech.iterator;

import com.awiatech.iterator.ArrayList;

/**
 * 测试类用于测试容器的功能。
 * @author Chicago
 *
 */
public class TestArrayList {

	public static void main(String[] args) {
		ArrayList al = new ArrayList();
		for(int i = 0; i < 15; i ++){
			al.add(new Object());
		}
		System.out.println(al.size());
	}

}

实现二:手动编写LinkedList

定义节点类数据结构

package com.awiatech.iterator;

public class Node {
	public Node(Object o, Node next) {
		super();
		this.data = o;
		this.next = next;
	}
	private Object data;
	public Object getData() {
		return data;
	}
	public void setData(int data) {
		this.data = data;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	private Node next;

}

package com.awiatech.iterator;

/**
 * 一个动态添加对象的容器,底层使用链表模拟。
 * @author Chicago
 *
 */
public class LinkedList {
	Node head = null; // 链表头
	Node tail = null; // 链表尾
	int size = 0; // 链表中元素个数
	
	/**
	 * 链表中添加元素
	 * @param o 要添加的元素
	 */
	public void add(Object o){
		Node n = new Node(o, null); // 构造要添加的节点
		// 若链表为空,则将链表头和尾都指向新节点
		if(head == null){
			head = n;
			tail = n;
		}
		tail.setNext(n); // 原链表尾指向新节点
		tail = n; // 链表尾重定向到新节点
		size ++; // 链表中元素数量累加
	}
	
	/**
	 * 链表中元素的个数
	 * @return 返回链表中元素的个数
	 */
	public int size(){
		return size;
	}

}

测试:

package com.awiatech.iterator;

import com.awiatech.iterator.LinkedList;

/**
 * 测试类用于测试容器的功能。
 * @author Chicago
 *
 */
public class TestLinkedList {

	public static void main(String[] args) {
		LinkedList al = new LinkedList();
		for(int i = 0; i < 15; i ++){
			al.add(new Object());
		}
		System.out.println(al.size());
	}

}

考虑到容器的可替换性。对程序进行优化,添加接口定义

package com.awiatech.iterator;

public interface Collection {
	public void add(Object o);
	public int size();
}

以上Node,ArrayList,LinkedList这三个类保持不变

测试:

package com.awiatech.iterator;

import com.awiatech.iterator.ArrayList;
import com.awiatech.iterator.LinkedList;

/**
 * 测试类用于测试容器的功能。
 * @author Chicago
 *
 */
public class Test {

	public static void main(String[] args) {
//		ArrayList al = new ArrayList();
//		LinkedList al = new LinkedList();
		
		// 面向接口编程,好处:灵活、可扩展
		Collection c = new ArrayList(); // 父类引用指向子类对象
		for(int i = 0; i < 15; i ++){
			c.add(new Object());
		}
		System.out.println(c.size());
	}

}


猜你喜欢

转载自blog.csdn.net/u010995220/article/details/51260389