So what this program does is processes ArrayList by using the Iterator method and prints them out.
I'm making my own Iterator by overriding and what I need help with is the remove method from Iterator.
Code:
public class MyArrayList implements Iterable<Object> {
public static final int DEFAULT_SIZE = 5;
public static final int EXPANSION = 5;
private int capacity;
private int size;
private Object[] items;
private int currentSize;
int modCount = 0;
int cursor = 0;
int lastRet = -1;
int expectedModCount = modCount;
public MyArrayList() {
size = 0;
capacity = DEFAULT_SIZE;
items = new Object[DEFAULT_SIZE];
this.currentSize = items.length;
}
@Override
public Iterator<Object> iterator() {
Iterator<Object> it = new Iterator<Object>() {
private int currentIndex = 0;
@Override
public boolean hasNext() {
try { return currentIndex <= currentSize && items[currentIndex] != null;
}catch(NoSuchElementException e){
System.out.println("There is nothing in the next element.");
}
return currentIndex <= currentSize && items[currentIndex] != null;
}
@Override
public Object next() {
checkForComodification();
try{
}catch(NoSuchElementException e){
System.out.println("There is nothing in the next element.");
}
return items[currentIndex++];
}
@Override
public void remove(){
if (lastRet< 0)
throw new IllegalStateException();
checkForComodification();
try {
MyArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
}catch (IndexOutOfBoundsException e){
throw new ConcurrentModificationException();
}
}
final void checkForComodification(){
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
};
return it;
}
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) {
try {
if (size >= capacity) this.expand();
items[size] = obj;
size++;
} catch (IndexOutOfBoundsException e) {
System.out.println("There is an error adding this word." + e.getMessage());
}
}
public int size() {
return size;
}
public Object get(int index) {
try {
return items[index];
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("ERROR- Cannot GET element. Index is out of range. Position: " +e.getMessage());
}
return items[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("ERROR- Cannot ADD element. Index out of range. Position: " +e.getMessage()+".");
}
}
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];
items[size] = null;
size--;
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.print("ERROR- Cannot REMOVE element. Index out of range. Position: " + e.getMessage());
}
return null;
}
public void set(int index, Object obj) {
try {
items[index] = obj;
}catch (IndexOutOfBoundsException e){
System.out.println("ERROR- Cannot SET word.. Index out of range. Position: "+e.getMessage());
}
}
}
Main method code:
class Task4Test {
static MyArrayList zoo = new MyArrayList();
public static void printZoo() {
System.out.print("The zoo now holds " + zoo.size() + " animals: ");
for (int j = 0; j < zoo.size(); j++) System.out.print(zoo.get(j) + " ");
System.out.println();
}
public static void main(String[] args) {
String[] zooList = {"Cheetah", "Jaguar", "Leopard", "Lion", "Panther", "Tiger"};
for (String x: zooList) zoo.add(x);
printZoo();
System.out.printf("\nTesting the iterator\n>> ");
Iterator it = zoo.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
System.out.printf("\nTesting the iterator again without resetting\n>> ");
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
System.out.printf("\nTesting the iterator again after resetting\n>> ");
it = zoo.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
System.out.printf("\nTesting for-each loop\n>> ");
for(Object animal: zoo) System.out.print(animal + " ");
System.out.println();
System.out.println("\nLetting all the animals escape");
while (zoo.size()>0) zoo.remove(0);
printZoo();
System.out.printf("\nTesting the iterator with an empty list\n>> ");
it = zoo.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
System.out.println("\nTest complete");
}
}
Now this prints out: The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger
Testing the iterator
>> Cheetah Jaguar Leopard Lion Panther Tiger
Testing the iterator again without resetting
>> //Is it supposed to be empty like this? (read below)
Testing the iterator again after resetting
>> Cheetah Jaguar Leopard Lion Panther Tiger
Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther Tiger
Letting all the animals escape
The zoo now holds 0 animals:
Testing the iterator with an empty list
>> Tiger //This is the main problem i'm trying to fix.
So for some reason Tiger always keep getting printed. Even when I change up loads of different methods. I have a feeling it could be something to do with Object remove(int index) method.
Furthermore, I understand after the "iterator without resetting" section there should be nothing but with my code shouldn't there be an Exception saying "There is nothing in the next element"?