坦克大战(一期)-设计模式-Iterator迭代器-添加泛型

DesignPatterns
bingor 2 years ago
parent b90e6bab97
commit 5e9a629668

@ -0,0 +1,27 @@
package com.msb.iterator.v4;/**
* @Author bingor
* @Date 2022/10/17 18:59
* @Description: com.msb.iterator.v4
* @Version: 1.0
*/
/**
*@ClassName Dog
*@Description TODO
*@Author bingor
*@Date 2022/10/17 18:59
*@Version 3.0
*/
public class Dog {
private int id;
public Dog(int id) {
this.id = id;
}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
'}';
}
}

@ -16,7 +16,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Collection_ list = new LinkedList_(); Collection_ list = new LinkedList_();
for (int i=0; i<15; i++) { for (int i=0; i<15; i++) {
list.add(i); list.add(new Dog(i));
} }
Iterator_ iterator = list.iterator(); Iterator_ iterator = list.iterator();

@ -0,0 +1,62 @@
package com.msb.iterator.v5;/**
* @Author bingor
* @Date 2022/10/17 17:06
* @Description: com.msb.iterator.v1
* @Version: 1.0
*/
import java.util.Collection;
/**
*@ClassName ArrayList_
*@Description TODO
*@Author bingor
*@Date 2022/10/17 17:06
*@Version 3.0
*/
public class ArrayList_<T> implements Collection_<T> {
private T[] objects = (T[]) new Object[10];
private int index = 0;
private int step = 0;
@Override
public void add(T o) {
if(index == objects.length) {
T[] newObjects = (T[]) new Object[2*objects.length];
System.arraycopy(objects, 0, newObjects, 0, objects.length);
objects = newObjects;
}
objects[index] = o;
index ++;
}
@Override
public int size() {
return index;
}
@Override
public Iterator_ iterator() {
return new ArrayListIterator();
}
private class ArrayListIterator implements Iterator_<T> {
private int currentIndex = 0;
@Override
public boolean hashNext() {
if(this.currentIndex >= objects.length) return false;
if(objects[this.currentIndex] == null) return false;
return true;
}
@Override
public T next() {
return objects[this.currentIndex++];
}
}
}

@ -0,0 +1,13 @@
package com.msb.iterator.v5;
/**
* @Author bingor
* @Date 2022/10/17 17:53
* @Description: com.msb.iterator.v3
* @Version: 1.0
*/
public interface Collection_<T> {
public void add(T o);
public int size();
public Iterator_<T> iterator();
}

@ -0,0 +1,12 @@
package com.msb.iterator.v5;
/**
* @Author bingor
* @Date 2022/10/17 18:00
* @Description: com.msb.iterator.v4
* @Version: 1.0
*/
public interface Iterator_<T> {
public boolean hashNext();
public T next();
}

@ -0,0 +1,74 @@
package com.msb.iterator.v5;/**
* @Author bingor
* @Date 2022/10/17 17:14
* @Description: com.msb.iterator.v2
* @Version: 1.0
*/
/**
*@ClassName LinkedList
*@Description
*@Author bingor
*@Date 2022/10/17 17:14
*@Version 3.0
*/
public class LinkedList_<T> implements Collection_<T> {
private int size = 0;
private Node head = null;
private Node tail = null;
@Override
public void add(T o) {
Node<T> node = new Node(o);
node.next = null;
if(head == null) {
head = node;
tail = node;
}
tail.next = node;
tail = node;
size++;
}
@Override
public int size() {
return size;
}
@Override
public Iterator_<T> iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator_<T> {
private Node currentNode = head;
@Override
public boolean hashNext() {
if(null == currentNode) return false;
return true;
}
@Override
public T next() {
T o = (T) currentNode.o;
currentNode = currentNode.next;
return o;
}
}
private class Node<T> {
private T o;
private Node next;
public Node(T o) {
this.o = o;
}
}
}

@ -0,0 +1,29 @@
package com.msb.iterator.v5;/**
* @Author bingor
* @Date 2022/10/17 17:54
* @Description: com.msb.iterator.v3
* @Version: 1.0
*/
import com.msb.iterator.v4.Dog;
/**
*@ClassName Main
*@Description TODO
*@Author bingor
*@Date 2022/10/17 17:54
*@Version 3.0
*/
public class Main {
public static void main(String[] args) {
Collection_ list = new LinkedList_();
for (int i=0; i<15; i++) {
list.add(new Dog(i));
}
Iterator_ iterator = list.iterator();
while (iterator.hashNext()) {
System.out.println(iterator.next());
}
}
}
Loading…
Cancel
Save