坦克大战(一期)-设计模式-Iterator迭代器1.0

DesignPatterns
bingor 2 years ago
parent cc02d545ff
commit b90e6bab97

@ -0,0 +1,44 @@
package com.msb.iterator.v1;/**
* @Author bingor
* @Date 2022/10/17 17:06
* @Description: com.msb.iterator.v1
* @Version: 1.0
*/
/**
*@ClassName ArrayList_
*@Description TODO
*@Author bingor
*@Date 2022/10/17 17:06
*@Version 3.0
*/
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[2*objects.length];
System.arraycopy(objects, 0, newObjects, 0, objects.length);
objects = newObjects;
}
objects[index] = o;
index ++;
}
public int size() {
return index;
}
}
class Main {
public static void main(String[] args) {
ArrayList_ list = new ArrayList_();
for (int i=0; i<15; i++) {
list.add(i);
}
System.out.println(list.size());
}
}

@ -0,0 +1,60 @@
package com.msb.iterator.v2;/**
* @Author bingor
* @Date 2022/10/17 17:14
* @Description: com.msb.iterator.v2
* @Version: 1.0
*/
import java.util.Arrays;
/**
*@ClassName LinkedList
*@Description
*@Author bingor
*@Date 2022/10/17 17:14
*@Version 3.0
*/
public class LinkedList_ {
private int size = 0;
private Node head = null;
private Node tail = null;
public void add(Object o) {
Node node = new Node(o);
node.next = null;
if(head == null) {
head = node;
tail = node;
}
tail.next = node;
tail = node;
size++;
}
public int size() {
return size;
}
private class Node {
private Object o;
private Node next;
public Node(Object o) {
this.o = o;
}
}
}
class Main {
public static void main(String[] args) {
LinkedList_ list = new LinkedList_();
for (int i=0; i<15; i++) {
list.add(i);
}
System.out.println(list.size());
}
}

@ -0,0 +1,36 @@
package com.msb.iterator.v3;/**
* @Author bingor
* @Date 2022/10/17 17:06
* @Description: com.msb.iterator.v1
* @Version: 1.0
*/
/**
*@ClassName ArrayList_
*@Description TODO
*@Author bingor
*@Date 2022/10/17 17:06
*@Version 3.0
*/
public class ArrayList_ implements Collection_ {
private Object[] objects = new Object[10];
private int index = 0;
@Override
public void add(Object o) {
if(index == objects.length) {
Object[] newObjects = 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;
}
}

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

@ -0,0 +1,51 @@
package com.msb.iterator.v3;/**
* @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_ implements Collection_ {
private int size = 0;
private Node head = null;
private Node tail = null;
@Override
public void add(Object o) {
Node 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;
}
private class Node {
private Object o;
private Node next;
public Node(Object o) {
this.o = o;
}
}
}

@ -0,0 +1,23 @@
package com.msb.iterator.v3;/**
* @Author bingor
* @Date 2022/10/17 17:54
* @Description: com.msb.iterator.v3
* @Version: 1.0
*/
/**
*@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(i);
}
System.out.println(list.size());
}
}

@ -0,0 +1,60 @@
package com.msb.iterator.v4;/**
* @Author bingor
* @Date 2022/10/17 17:06
* @Description: com.msb.iterator.v1
* @Version: 1.0
*/
/**
*@ClassName ArrayList_
*@Description TODO
*@Author bingor
*@Date 2022/10/17 17:06
*@Version 3.0
*/
public class ArrayList_ implements Collection_ {
private Object[] objects = new Object[10];
private int index = 0;
private int step = 0;
@Override
public void add(Object o) {
if(index == objects.length) {
Object[] newObjects = 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_ {
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 Object next() {
return objects[this.currentIndex++];
}
}
}

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

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

@ -0,0 +1,74 @@
package com.msb.iterator.v4;/**
* @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_ implements Collection_ {
private int size = 0;
private Node head = null;
private Node tail = null;
@Override
public void add(Object o) {
Node 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_ iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator_ {
private Node currentNode = head;
@Override
public boolean hashNext() {
if(null == currentNode) return false;
return true;
}
@Override
public Object next() {
Object o = currentNode.o;
currentNode = currentNode.next;
return o;
}
}
private class Node {
private Object o;
private Node next;
public Node(Object o) {
this.o = o;
}
}
}

@ -0,0 +1,27 @@
package com.msb.iterator.v4;/**
* @Author bingor
* @Date 2022/10/17 17:54
* @Description: com.msb.iterator.v3
* @Version: 1.0
*/
/**
*@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(i);
}
Iterator_ iterator = list.iterator();
while (iterator.hashNext()) {
System.out.println(iterator.next());
}
}
}
Loading…
Cancel
Save