import java.util.*; public class SetShipGeneric implements Iterable { private ArrayList _places; private final int _maxCount; public int getCount() { return _places.size(); } public SetShipGeneric(int count) { _maxCount = count; _places = new ArrayList<>(); } public int Insert(T ship) { return Insert(ship, 0); } public int Insert(T ship, int position) { if (position < 0 || position > getCount() || _maxCount == getCount()) return -1; _places.add(position,ship); return position; } public T Remove(int position) { if (position < 0 || position >= getCount()) return null; T DelElement = (T) _places.get(position); _places.remove(position); return DelElement; } public T Get(int position) { if (position < 0 || position >= getCount()) return null; return _places.get(position); } public void Set(int position,T value) { if (position < _maxCount || position >= 0) { Insert(value, position); } } @Override public Iterator iterator() { return _places.iterator(); } }