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,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,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,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…
Reference in new issue