diff --git a/src/main/java/com/msb/iterator/v4/Dog.java b/src/main/java/com/msb/iterator/v4/Dog.java new file mode 100644 index 0000000..0efd176 --- /dev/null +++ b/src/main/java/com/msb/iterator/v4/Dog.java @@ -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 + + '}'; + } +} diff --git a/src/main/java/com/msb/iterator/v4/Main.java b/src/main/java/com/msb/iterator/v4/Main.java index fe93066..9db4aa0 100644 --- a/src/main/java/com/msb/iterator/v4/Main.java +++ b/src/main/java/com/msb/iterator/v4/Main.java @@ -16,7 +16,7 @@ public class Main { public static void main(String[] args) { Collection_ list = new LinkedList_(); for (int i=0; i<15; i++) { - list.add(i); + list.add(new Dog(i)); } Iterator_ iterator = list.iterator(); diff --git a/src/main/java/com/msb/iterator/v5/ArrayList_.java b/src/main/java/com/msb/iterator/v5/ArrayList_.java new file mode 100644 index 0000000..e533baf --- /dev/null +++ b/src/main/java/com/msb/iterator/v5/ArrayList_.java @@ -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_ implements Collection_ { + + 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_ { + 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++]; + } + + } + + +} diff --git a/src/main/java/com/msb/iterator/v5/Collection_.java b/src/main/java/com/msb/iterator/v5/Collection_.java new file mode 100644 index 0000000..3d18081 --- /dev/null +++ b/src/main/java/com/msb/iterator/v5/Collection_.java @@ -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_ { + public void add(T o); + public int size(); + public Iterator_ iterator(); +} diff --git a/src/main/java/com/msb/iterator/v5/Iterator_.java b/src/main/java/com/msb/iterator/v5/Iterator_.java new file mode 100644 index 0000000..b904662 --- /dev/null +++ b/src/main/java/com/msb/iterator/v5/Iterator_.java @@ -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_ { + public boolean hashNext(); + public T next(); +} diff --git a/src/main/java/com/msb/iterator/v5/LinkedList_.java b/src/main/java/com/msb/iterator/v5/LinkedList_.java new file mode 100644 index 0000000..749abb9 --- /dev/null +++ b/src/main/java/com/msb/iterator/v5/LinkedList_.java @@ -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_ implements Collection_ { + + private int size = 0; + private Node head = null; + private Node tail = null; + + @Override + public void add(T 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 T next() { + T o = (T) currentNode.o; + currentNode = currentNode.next; + return o; + } + } + + private class Node { + private T o; + private Node next; + + public Node(T o) { + this.o = o; + } + } + +} + diff --git a/src/main/java/com/msb/iterator/v5/Main.java b/src/main/java/com/msb/iterator/v5/Main.java new file mode 100644 index 0000000..67122d4 --- /dev/null +++ b/src/main/java/com/msb/iterator/v5/Main.java @@ -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()); + } + } +}