您的位置:首页 > 其它

设计模式——容器(iterator) 学习笔记

2014-07-27 17:40 281 查看
主要用在容器的遍历上;; 容器是指能够容纳其他对象的

模拟数组的容器

package com.bjsxt.iterator;

public class ArrayList {

private Object [] objects = new Object[10];

private int index = 0;

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 ++;

}

public int size() {

return index;

}

}

package com.bjsxt.iterator;

import com.bjsxt.iterator.ArrayList;

public class Test {

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());

}

}

模拟链表的容器

package com.bjsxt.iterator;

public class LinkedList {

//记录节点的开始位置

Node head = null;

//记录上一次最后一个节点的位置

Node tail = null;

int size = 0;

public void add(Object o) {

Node n = new Node(o , null);

if(head == null) { //说明该节点是第一个节点

head = n;

tail = n;

}

//如果不是第一个节点,,未加该节点之前的最后一个节点的next应该指向新加的节点

tail.setNext(n);

//并且此时,,最后一个节点是新加的节点,,所以应该将tail 改成 新加的节点 n

tail = n;

size ++;

}

public int size () {

return size;

}

}

接口中所有方法都是public

当考虑想把ArrayList 和 LinkedList 封装起来时

想到了用接口Collection

package com.bjsxt.iterator;

public interface Collection {

void add(Object o) ;

int size();

}

package com.bjsxt.iterator;

import com.bjsxt.iterator.ArrayList;

public class Test {

public static void main(String[] args) {

LinkedList ll = new LinkedList();

//面向接口編程 ,, 這樣更零活 ,,,如果写在配置文件中 会更加的方便

Collection c = new ArrayList();

Collection c2 = new LinkedList();

for(int i = 0; i < 15 ; i ++) {

c.add(new Cat(i));

}

System.out.println(c.size());

}

}

当想到要遍历ArrayList 和 ListedList 时 , 想到了可以再接口中添加一个方法2 showAll();

package com.bjsxt.iterator;
import com.bjsxt.iterator.Collection;

public class ArrayList implements Collection{
private Object [] objects = new Object[10];

private int index = 0;

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 ++;

}

public int size() {

return index;

}

public void showAll() {

for(int i = 0 ; i < index; i++){

Object b = objects[i];

System.out.println(b);

}

}

}

package com.bjsxt.iterator;

import com.bjsxt.iterator.Collection;
public class LinkedList implements Collection{

//记录节点的开始位置

Node head = null;

//记录上一次最后一个节点的位置

Node tail = null;

int size = 0;
public void add(Object o) {
Node n = new Node(o , null);

if(head == null) { //说明该节点是第一个节点

head = n;

tail = n;

}

//如果不是第一个节点,,未加该节点之前的最后一个节点的next应该指向新加的节点

tail.setNext(n);

//并且此时,,最后一个节点是新加的节点,,所以应该将tail 改成 新加的节点 n

tail = n;

size ++;

}

public int size () {

return size;

}

public void showAll() {

Node b;

b = head;

while(b.getNext() != null) {

System.out.println(b.getData());

b = b.getNext();

}

}

}

package com.bjsxt.iterator;
public interface Collection {
void add(Object o) ;

int size();

void showAll();

}

还有一种更好的方法:

package com.bjsxt.iterator;
import com.bjsxt.iterator.Collection;

public class ArrayList implements Collection{
private Object [] objects = new Object[10];

private int index = 0;

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 ++;

}

public int size() {

return index;

}

public void showAll() {

for(int i = 0 ; i < index; i++){

Object b = objects[i];

System.out.println(b);

}

}

//一種更好的方法 {好在 定义的接口更加的细致}

public Iterator iterator() {

return new ArrayListIterator();

}

private class ArrayListIterator implements Iterator {
private int currentIndex = 0;

@Override

public boolean hasNext() {

if(currentIndex >= index )

return false;

return true;

}
@Override

public Object next() {

Object tmp = objects[currentIndex];

currentIndex ++;

return tmp;

}

}

}

package com.bjsxt.iterator;

import com.bjsxt.iterator.Collection;
public class LinkedList implements Collection{

//记录节点的开始位置

Node head = null;

//记录上一次最后一个节点的位置

Node tail = null;

int size = 0;
public void add(Object o) {
Node n = new Node(o , null);

if(head == null) { //说明该节点是第一个节点

head = n;

tail = n;

}

//如果不是第一个节点,,未加该节点之前的最后一个节点的next应该指向新加的节点

tail.setNext(n);

//并且此时,,最后一个节点是新加的节点,,所以应该将tail 改成 新加的节点 n

tail = n;

size ++;

}

public int size () {

return size;

}

public void showAll() {

Node b;

b = head;

while(b.getNext() != null) {

System.out.println(b.getData());

b = b.getNext();

}

}
@Override

public Iterator iterator() {

return new LinkedListIterator();

}

private class LinkedListIterator implements Iterator {

Node currentNode = head ;

@Override

public boolean hasNext() {

if(currentNode == null) {

return false;

}

return true;

}
@Override

public Object next() {

//取到當前的節點

Object tmp = currentNode;

//將該節點指針向下移一個

currentNode = currentNode.getNext();

return tmp;

}

}

}

package com.bjsxt.iterator;
public interface Collection {
void add(Object o) ;

int size();

void showAll();

Iterator iterator();

}

package com.bjsxt.iterator;
public interface Iterator {
Object next();

boolean hasNext();

}

package com.bjsxt.iterator;
import com.bjsxt.iterator.ArrayList;

public class Test {
public static void main(String[] args) {
LinkedList ll = new LinkedList();

//面向接口編程 ,, 這樣更零活

Collection c = new ArrayList();

Collection c2 = new LinkedList();

for(int i = 0; i < 15 ; i ++) {

c.add(new Cat(i));

}

System.out.println(c.size());

Iterator it = c.iterator();

while(it.hasNext()) {

Object o = it.next();

System.out.println(o + "");

}

}

}



下面是泛型的一个例子: {泛型有点像C++中的模板}

package com.bjsxt.iterator;
//泛型

public class GenericArrayList<Element> {
private Object [] objects = new Object[10];

private int index = 0;

public void add (Element 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 ++;

}

public int size() {

return index;

}

public static void main(String args[]) {

GenericArrayList<String> a = new GenericArrayList<String>();

a.add("String");

}

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: