package laba1Loco; public class SetGeneric { /// /// Массив объектов, которые храним /// private Object[] _places; /// /// Количество объектов в массиве /// public int Count; /// /// Конструктор /// /// public SetGeneric(int count) { _places = new Object[count]; Count = _places.length; } /// /// Добавление объекта в набор /// /// Добавляемый поезд /// public int Insert(T train) { int i = 0; for (;i < _places.length; i++) { if (_places[i] == null) break; } if (i == _places.length) return -1; for (; i > 0; i--) { _places[i] = _places[i - 1]; } _places[i] = train; return i; } /// /// Добавление объекта в набор на конкретную позицию /// /// Добавляемый поезд /// Позиция /// public boolean Insert(T train, int position) { if (position < 0 || position >= _places.length) return false; for (; position < _places.length; position++) { if (_places[position] == null) break; } if (position == _places.length) return false; for (; position > 0; position--) { _places[position] = _places[position - 1]; } _places[position] = train; return true; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// public boolean Remove(int position) { if (position < 0 || position >= _places.length) return false; _places[position] = null; return true; } /// /// Получение объекта из набора по позиции /// /// /// public T Get(int position) { if (position < 0 || position >= _places.length) return null; return (T)_places[position]; } }