2022-11-05 13:03:36 +04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2022-10-21 16:04:51 +04:00
|
|
|
public class SetLocomotivesGeneric <T>
|
|
|
|
{
|
2022-11-05 13:03:36 +04:00
|
|
|
/// Список хранимых объектов
|
|
|
|
private final ArrayList<T> _places;
|
2022-10-21 16:04:51 +04:00
|
|
|
|
|
|
|
public int Count() {
|
2022-11-05 13:03:36 +04:00
|
|
|
return _places.size();
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-11-05 13:03:36 +04:00
|
|
|
// Ограничение на количество
|
|
|
|
private final int _maxCount;
|
|
|
|
|
2022-10-21 16:04:51 +04:00
|
|
|
public SetLocomotivesGeneric(int count) {
|
2022-11-05 13:03:36 +04:00
|
|
|
_maxCount = count;
|
|
|
|
_places = new ArrayList<>();
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-10-21 21:59:22 +04:00
|
|
|
public int Insert (T locomotive) {
|
2022-10-21 16:04:51 +04:00
|
|
|
return Insert(locomotive, 0);
|
|
|
|
}
|
|
|
|
|
2022-10-21 21:59:22 +04:00
|
|
|
public int Insert (T locomotive, int position) {
|
2022-11-05 13:03:36 +04:00
|
|
|
if (position >= _maxCount|| position < 0) return -1;
|
|
|
|
_places.add(position, locomotive);
|
2022-10-21 21:59:22 +04:00
|
|
|
return position;
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-10-21 21:59:22 +04:00
|
|
|
public T Remove (int position) {
|
2022-11-05 13:03:36 +04:00
|
|
|
if (position >= _maxCount || position < 0) return null;
|
|
|
|
T result = _places.get(position);
|
|
|
|
_places.remove(position);
|
2022-10-21 21:59:22 +04:00
|
|
|
return result;
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public T Get(int position)
|
|
|
|
{
|
2022-11-05 13:03:36 +04:00
|
|
|
if (position >= _maxCount || position < 0)
|
2022-10-21 16:04:51 +04:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-05 13:03:36 +04:00
|
|
|
return _places.get(position);
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-11-05 13:03:36 +04:00
|
|
|
|
|
|
|
/// Проход по набору до первого пустого
|
|
|
|
public Iterable<T> GetLocomotives()
|
|
|
|
{
|
|
|
|
/*for (var locomotive : _places)
|
|
|
|
{
|
|
|
|
if (locomotive != null)
|
|
|
|
{
|
|
|
|
yield return locomotive;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
return _places;
|
|
|
|
}
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|