[英]adding an iterator to my arraylist
import java.util.Iterator;
public class MyArrayList<E> implements Iterable<E> {
public static final int DEFAULT_SIZE = 5;
public static final int EXPANSION = 5;
private int capacity;
private int size;
private Object[] items;
public MyArrayList() {
size = 0;
capacity = DEFAULT_SIZE;
items = new Object[DEFAULT_SIZE];
}
private void expand() {
Object[] newItems = new Object[capacity + EXPANSION];
for (int j = 0; j < size; j++) newItems[j] = items[j];
items = newItems;
capacity = capacity + EXPANSION;
}
public void add(Object obj) {
if (size >= capacity) this.expand();
items[size] = obj;
size++;
}
public int size() {
return size;
}
public Object get(int index) {
try{
return items[index];
} catch(IndexOutOfBoundsException e){
System.out.println("Exception Thrown: " + "Index is out of bound");
}
return index;
}
public boolean contains(Object obj) {
for (int j = 0; j < size; j++) {
if (obj.equals(this.get(j))) return true;
}
return false;
}
public void add(int index, Object obj) {
try{
if (size >= capacity) this.expand();
for (int j = size; j > index; j--) items[j] = items[j - 1];
items[index] = obj;
size++;
} catch(IndexOutOfBoundsException e){
System.out.println("Exception Thrown: " + "Index is out of bound");
}
return;
}
public int indexOf(Object obj) {
for (int j = 0; j < size; j++) {
if (obj.equals(this.get(j))) return j;
}
return -1;
}
public boolean remove(Object obj) {
for (int j = 0; j < size; j++) {
if (obj.equals(this.get(j))) {
for (int k = j; k < size-1; k++) items[k] = items[k + 1];
size--;
items[size] = null;
return true;
}
}
return false;
}
public Object remove(int index) {
try{
Object result = this.get(index);
for (int k = index; k < size-1; k++) items[k] = items[k + 1];
items[size] = null;
size--;
return result;
} catch(IndexOutOfBoundsException e){
System.out.println("Exception Thrown: " + "Index is out of bound");
}
return index;
}
public void set(int index, Object obj) {
try{
items[index] = obj;
} catch(IndexOutOfBoundsException e){
System.out.println("Exception Thrown: " + "Index is out of bound");
}
return;
}
public Iterator<E> iterator() {
return new MyIterator<E>();
}
public class MyIterator <T> implements Iterator<T>{
public boolean hasNext(){
}
public T next(){
}
public void remove(){
}
}
}
Basically I'm trying to improve the functionality of my arraylist, as it uses for loops for methods such as add and remove, however I am trying to use an iterator instead and I searched it up and I found out you cannot just simply add implements iterable to the main class, it has to be implemented by using three methods next(), hasNext() and remove(). 基本上,我正在尝试改善arraylist的功能,因为它用于诸如add和remove之类的方法的循环,但是我尝试使用迭代器,因此我进行了搜索,发现您不能只是简单地添加实现迭代到主类,必须使用next(),hasNext()和remove()这三种方法来实现。 I added the three methods at the bottom of the code but i'm really not sure how I implement it in order for it to begin to work.
我在代码底部添加了三个方法,但我真的不确定如何实现它才能使其开始工作。
You'll need to keep track of the index in the items
array that the Iterator
is on. 您需要跟踪
Iterator
所在的items
数组中的索引。 Let's call it int currentIndex
. 我们称之为
int currentIndex
。 hasNext()
will return true
if currentIndex < size
. 如果
currentIndex < size
则hasNext()
将返回true
。 next()
will increment currentIndex
if hasNext()
is true
and return items[currentIndex]
, otherwise it should throw an Exception
, say NoSuchElementException
. 如果
hasNext()
为true
,则hasNext()
next()
将递增currentIndex
并返回items[currentIndex]
,否则它将引发Exception
,例如NoSuchElementException
。 Remove will call remove(currentIndex)
. Remove将调用
remove(currentIndex)
。
You need to pass the items
array to your MyIterator
class so that you can keep track of the current position of the cursor in the array. 您需要将
items
数组传递给MyIterator
类,以便可以跟踪光标在数组中的当前位置。 Now based on the current position of the cursor you could implement all the abstract methods. 现在,基于光标的当前位置,您可以实现所有抽象方法。
In the constructor of the MyIterator
class pass the array as a parameter as public MyIterator(E[] array)
and store the array as a local variable. 在
MyIterator
类的构造函数中,将数组作为参数传递为public MyIterator(E[] array)
并将该数组存储为局部变量。 also create a local variable cursor and set its value to 0. 还创建一个局部变量游标并将其值设置为0。
Here is an example (NOTE: I have not tried to compile this or anything so please update this post if you find any errors!) 这是一个示例(注意:我尚未尝试编译此文件或任何东西,因此,如果发现任何错误,请更新此帖子!)
public class MyArrayList<E> implements Iterable<E> {
...
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
private Object[] currentData = items;
private int pos = 0;
@Override
public boolean hasNext() {
return pos < currentData.length;
}
@Override
public E next() {
return (E) currentData[pos++];
}
@Override
public void remove() {
MyArrayList.this.remove(pos++);
}
};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.