1

I must implement all methods of collection using iterator only.

My iterator:

class CustomIterator<T> implements Iterator {

        private int index;

        @Override
        public boolean hasNext() {
            return index < data.size();
        }
        @Override
        public T next() {
            return (T)data.get(index);
        }
        public void remove () {
            data.remove(index);
        }
    }

data, it is arraylist in my own collection.

For example, method size implement in my code:

public class CustomCollections<T> implements Collection {
public ArrayList<T> data;
 public int size() {
        CustomIterator iterator = new CustomIterator<>();
        int length= 0;
        while (iterator.hasNext()) {
            length++;
        }
        return length;
    }
   }

But i don't know, how to implement method .add?

Please help me in that problem.

Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31
Valeriu
  • 171
  • 1
  • 1
  • 11

0 Answers0