PIbd-23_Mochalov_D.V._Locom.../SetLocomotivesGeneric.java

64 lines
1.5 KiB
Java

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