package ProjectElectricLocomotive; import java.util.ArrayList; import java.util.Iterator; import java.util.NoSuchElementException; public class SetGeneric{ private ArrayList _places; public int Count() { return _places.size(); } public int maxCount; public SetGeneric(int count) { maxCount = count; _places = new ArrayList(count); } public int Insert(T loco) { return Insert(loco, 0); } public int Insert(T loco, int position) { if(position < 0 || position > Count()) return -1; else { _places.add(position, loco); return position; } } public T Remove(int position) { if (position >= Count() || position < 0) return null; else { T plane = _places.get(position); _places.remove(position); _places.set(position, null); return plane; } } public T Get(int position) { if (position < 0 || position >= Count()) return null; return _places.get(position); } public void Set(int position, T loco) { // Проверка позиции // Проверка свободных мест в списке if (position < 0 || position >= maxCount || Count() == maxCount) { return; } // Вставка в список по позиции _places.set(position, loco); } public ArrayList GetEnumerator() { return _places; } /* public Iterable GetLocomotives(final Integer maxLocomotives) { return new Iterable() { @Override public Iterator iterator() { return new Iterator() { private int currentIndex = 0; private int count = 0; //есть ли в коллекции еще место (значения) @Override public boolean hasNext() { return currentIndex < _places.size() && (maxLocomotives == null || count < maxLocomotives); } //ну и, соответственно, переходим к следующему, если хэз некст == true @Override public T next() { if (hasNext()) { count++; return _places.get(currentIndex++); } throw new NoSuchElementException(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; } };*/ }