diff --git a/src/main/java/com/msb/iterator/v1/ArrayList_.java b/src/main/java/com/msb/iterator/v1/ArrayList_.java new file mode 100644 index 0000000..eaecef4 --- /dev/null +++ b/src/main/java/com/msb/iterator/v1/ArrayList_.java @@ -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()); + } +} diff --git a/src/main/java/com/msb/iterator/v2/LinkedList_.java b/src/main/java/com/msb/iterator/v2/LinkedList_.java new file mode 100644 index 0000000..dc9f0f3 --- /dev/null +++ b/src/main/java/com/msb/iterator/v2/LinkedList_.java @@ -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()); + } +} diff --git a/src/main/java/com/msb/iterator/v3/ArrayList_.java b/src/main/java/com/msb/iterator/v3/ArrayList_.java new file mode 100644 index 0000000..991b9ea --- /dev/null +++ b/src/main/java/com/msb/iterator/v3/ArrayList_.java @@ -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; + } + +} diff --git a/src/main/java/com/msb/iterator/v3/Collection_.java b/src/main/java/com/msb/iterator/v3/Collection_.java new file mode 100644 index 0000000..40fdb41 --- /dev/null +++ b/src/main/java/com/msb/iterator/v3/Collection_.java @@ -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(); +} diff --git a/src/main/java/com/msb/iterator/v3/LinkedList_.java b/src/main/java/com/msb/iterator/v3/LinkedList_.java new file mode 100644 index 0000000..053f42e --- /dev/null +++ b/src/main/java/com/msb/iterator/v3/LinkedList_.java @@ -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; + } + } + +} + diff --git a/src/main/java/com/msb/iterator/v3/Main.java b/src/main/java/com/msb/iterator/v3/Main.java new file mode 100644 index 0000000..3c24daf --- /dev/null +++ b/src/main/java/com/msb/iterator/v3/Main.java @@ -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()); + } +} diff --git a/src/main/java/com/msb/iterator/v4/ArrayList_.java b/src/main/java/com/msb/iterator/v4/ArrayList_.java new file mode 100644 index 0000000..9d60de5 --- /dev/null +++ b/src/main/java/com/msb/iterator/v4/ArrayList_.java @@ -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++]; + } + + } + + +} diff --git a/src/main/java/com/msb/iterator/v4/Collection_.java b/src/main/java/com/msb/iterator/v4/Collection_.java new file mode 100644 index 0000000..b919d0f --- /dev/null +++ b/src/main/java/com/msb/iterator/v4/Collection_.java @@ -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(); +} diff --git a/src/main/java/com/msb/iterator/v4/Iterator_.java b/src/main/java/com/msb/iterator/v4/Iterator_.java new file mode 100644 index 0000000..56f74e7 --- /dev/null +++ b/src/main/java/com/msb/iterator/v4/Iterator_.java @@ -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(); +} diff --git a/src/main/java/com/msb/iterator/v4/LinkedList_.java b/src/main/java/com/msb/iterator/v4/LinkedList_.java new file mode 100644 index 0000000..1dbeda3 --- /dev/null +++ b/src/main/java/com/msb/iterator/v4/LinkedList_.java @@ -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; + } + } + +} + diff --git a/src/main/java/com/msb/iterator/v4/Main.java b/src/main/java/com/msb/iterator/v4/Main.java new file mode 100644 index 0000000..fe93066 --- /dev/null +++ b/src/main/java/com/msb/iterator/v4/Main.java @@ -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()); + } + } +}