将迭代器添加到我的数组列表
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(){
}
}
}
基本上我正在尝试改进我的 arraylist 的功能,因为它使用 for 循环来处理添加和删除等方法,但是我正在尝试使用迭代器来代替,我搜索了它并发现了你不能简单地在main class 中添加可迭代的实现,它必须使用next()、hasNext() 和remove() 三个方法来实现。我在代码底部添加了这三种方法,但我真的不确定如何实现它才能开始工作。
您需要跟踪 items
数组中 Iterator
所在的索引。我们称它为 int currentIndex
。如果 currentIndex < size
,hasNext()
将 return true
。如果 hasNext()
是 true
和 return items[currentIndex]
,next()
将增加 currentIndex
,否则它应该抛出一个 Exception
,比如 NoSuchElementException
。删除将调用 remove(currentIndex)
.
您需要将 items
数组传递给您的 MyIterator
class 以便您可以跟踪光标在数组中的当前位置。现在基于光标的当前位置你可以实现所有的抽象方法。
在 MyIterator
class 的构造函数中将数组作为参数传递为 public MyIterator(E[] array)
并将数组存储为局部变量。还创建一个局部变量 cursor 并将其值设置为 0.
这是一个例子(注意:我没有尝试编译这个或任何东西所以如果你发现任何错误请更新这个post!)
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++);
}
};
}
}
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(){
}
}
}
基本上我正在尝试改进我的 arraylist 的功能,因为它使用 for 循环来处理添加和删除等方法,但是我正在尝试使用迭代器来代替,我搜索了它并发现了你不能简单地在main class 中添加可迭代的实现,它必须使用next()、hasNext() 和remove() 三个方法来实现。我在代码底部添加了这三种方法,但我真的不确定如何实现它才能开始工作。
您需要跟踪 items
数组中 Iterator
所在的索引。我们称它为 int currentIndex
。如果 currentIndex < size
,hasNext()
将 return true
。如果 hasNext()
是 true
和 return items[currentIndex]
,next()
将增加 currentIndex
,否则它应该抛出一个 Exception
,比如 NoSuchElementException
。删除将调用 remove(currentIndex)
.
您需要将 items
数组传递给您的 MyIterator
class 以便您可以跟踪光标在数组中的当前位置。现在基于光标的当前位置你可以实现所有的抽象方法。
在 MyIterator
class 的构造函数中将数组作为参数传递为 public MyIterator(E[] array)
并将数组存储为局部变量。还创建一个局部变量 cursor 并将其值设置为 0.
这是一个例子(注意:我没有尝试编译这个或任何东西所以如果你发现任何错误请更新这个post!)
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++);
}
};
}
}